Undefined in php with ajax

0

The console shows me that the variable has not been defined 'undefined' my js

function UpdateUser(str){
var id = str;
var name = $('#nm-'+str).val();

$.ajax({
    type: "POST",
    url: "app/bin/adduser.php?p=update",
    data: "id="+id+"&nm="+name,
    success: function(data){
        console.log(data);
        console.log(name);
        console.log(id);
        viewUser();
    }
})}

the input to send the data

 <div class="modal-body">
                  <input type="hidden" id="<?php echo $user->id; ?>" >
                    <div class="form-group">
                        <label for="nm">Nombre</label>
                        <input type="text" class="form-ivss" id="nm-<?php echo $user->name; ?>" value="<?php echo $user->name; ?>">
                    </div>

Where I get the data

elseif ($page == 'update'){
$id = $_POST['id'];
$name = $_POST['nm'];
echo $name;
echo $id;

P.D: it is worth noting that I receive the id but not the name

    
asked by Josbert Hernandez 08.02.2017 в 18:16
source

3 answers

0

Look, that you concatenate in a single line at the time of sending data better send each data in its respective variable, try this for your ajax

function UpdateUser(str){

    $.ajax({
        type: "POST",
        url: "app/bin/adduser.php?p=update",
        dataType : "json",
        data: {
              Update:{
                  id: $('#idUsuario').val(),
                  nombre: $('#nombreUsuario').val()
              },
        },
        success: function(data){
            console.log(data);
            console.log(name);
            console.log(id);
            viewUser();
        }
    })}

In the case of your form, there is no need to do it as you do, better verify that the data is sent and that it is a conventional but functional code to avoid that of concatenations, etc. instead of what you have and if you like you can try this ...

<div class="modal-body">
    <input type="hidden" id="idUsuario" value="<?php echo $user->id; ?>" >
        <div class="form-group">
            <label for="nm">Nombre</label>
            <input type="text" class="form-ivss" id="nombreUsuario" value="<?php echo $user->name; ?>">
        </div>
</div>

and in the end you send it to call as follows ..

elseif ($page == 'update'){
$id = $_POST['Update']['id'];
$name = $_POST['Update']['nombre'];
echo $name;
echo $id;

Well, it's just my opinion, you can take that example and try it on your system if it's your liking ... I hope I could have helped you .. Greetings

    
answered by 08.02.2017 в 19:02
0

The problem comes in that in the html to the input name you define it as "nm-<?php echo user->name;?>" and in the js in str you send the id that is why you throw the undefined and try to access the name with this id, so that it works for you modify the input name "nm-<?php echo user->id;?>" to work for you

I proceed to give you a brief explanation step by step of the meaning of what you are trying to do and what would be the correct way to express it
1- type: "POST" means that the type of request is through POST, therefore the correct way to make the request is as follows:

function UpdateUser(str){

$.ajax({
    // especifica si será una petición POST o GET
    type: "POST",
    //URL de la petición
    url: "app/bin/adduser.php",
    // el tipo de información que se espera de respuesta
    dataType : "json",
    // la información a enviar
    // (esta puede ser una cadena de datos)
    data: {
              p:'update'
              id: id,
              nm:name

    }....

2 dataType:'json' indicates that the response of the ajax must be of this type therefore from php we return the result json_encode($array) :

    $page = $_POST['p'];
    elseif ($page == 'update'){
    $id = $_POST['id'];
    $name = $_POST['nombre'];
    $array = array(
    "name"    => $name,
    "address"  => $id,
   );
   //Retorna la representación JSON del valor dado
   echo json_encode($array);

3- Finally in the function that we define in the success we obtain what the php sends us and we can access the id and name in this way:

 // código a ejecutar si la petición es satisfactoria;
// la respuesta es pasada como argumento a la función
success: function(data){
    console.log(data);
    console.log(data.name);
    console.log(data.id);
    viewUser();
}
    
answered by 09.02.2017 в 20:40
0

You just have to change it here:

 <div class="modal-body">
                  <input type="hidden" id="<?php echo $user->id; ?>" >
                    <div class="form-group">
                        <label for="nm">Nombre</label>
                        <input type="text" class="form-ivss" id="nm-<?php echo $user->name; ?>" value="<?php echo $user->name; ?>">
                    </div>

When you pass the str by parameter you are passing the numeric ID that works for you, since in the input you had to also put that $user->id in id="nm-<?php echo $user->name; ?>" , if you put $user->name you will never be picked up that value, it has to be $user->id remaining so <input type="text" class="form-ivss" id="nm-<?php echo $user->id; ?>" value="<?php echo $user->name; ?>">

    
answered by 11.07.2018 в 14:34