Metio_1993 Metio_1993 - 23 days ago 11
HTML Question

How to update a table from Ajax post callback Symfony 3

I have a table that needs to be updated by Ajax callback, I'm really struggling to find the right way to do so.

here is a picture how the table should look like:
enter image description here

I cannot find the right way to so, here is my current HTML script:

<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>12NC</th>
<th>Object Description</th>

</tr>
</thead>
<tbody>
<tr>
<th scope="row"><input id="ID"></th>
<td><input id="NC"></td>
<td><input id="Des"></td>
</tr>

</tbody>


And here is the callbacks from Ajax:

person_name:$('#input_text').val()
} ,
function (data)
{
obj1 = data[0];
obj2 = data[1];

var obj = JSON.parse(obj2);
var obj1 = JSON.parse(obj1);



$('#ID').val(obj1[1].id);
$('#NC').val(obj1[1].n_c);
$('#Des').val(obj[1].description);
}
);
})
}
);


can someone please tell me if I'm doing it right and how I can send all the values from because now I can send only value 1 from the array.

Answer

you need a success function in ajax. easier option would be to echo the value from the php page where ajax response is sent. such as

echo '<tr>
        <td scope="row">' . $idValue . '</th>
        <td>' . $12ncValue . '</td>
        <td>' . $descriptionValue . '</td>
      </tr>';

and in the success function you can append to the tbody after you give it a class or id.

<tbody id="tbody">


success: function (response) {
                    $('#tbody').append(response);
                   }

as for another approach would be declare an array of variables, and

echo json_encode($array)

and with .each function you can loop the data from the ajax success function.

Edit: if you get the value. you need to append them. give id to tbody

</tbody id="tbody">

and in function(data)

 $('#tbody').append('<tr><td>'+obj1[1].id+'</td><td>'+obj1[1].n_c+'</td><td>'+obj[1].description+'</td></tr>');
Comments