javascript - How to append the values in table using AJAX


Here i am passing javascript varaible into PHP function using AJAX ,here it will be working fine,** console.log(fname);** Here i got all values but append the tables means i am getting [object Object],how can solve this error

<script type="text/javascript">
    $("#reservation").on("change", function() {
       var reservation = $(this).val();
              type: 'post',
              url: 'date-range.php',
              data: {
                  logindate: reservation,
              success: function( data ) {
              var res=jQuery.parseJSON(data);// convert the json

                  var htmlString='';

                   $.each( res['data'], function( key, value ) {

                   var ssm_id = value.ssm_id; // here i got ssmid
                   htmlString+='<td>'+ $.ajax({
                    type: 'post',
                    url: 'config/functions.php',
                   data: {
                        ssm_id: ssm_id,
                    success: function( fname ) {
                    console.log(fname);//HERE I GOT ALL VALUES
                    htmlString+='<td>'+fname+'</td>';// BUT HERE I CAN'T APPENT THE VALUES IN TABLE 
                   htmlString+='<td>'+ "<span style='color:green'>View Profile</span>"+'</td>';

                  /* htmlString+='<td>'+ "<span style='color:green'>Completed</span>"+'</td>';*/

                   $('#datatable-editable > tbody').empty().append(htmlString);
                  $('#datatable-editable > tbody').empty().append("<center style='height:100px;padding-top:36px;color:red;font-size:17px;'><b>No matching records found</b></center>");



$ssm_id = $_POST['ssm_id'];
    echo firstname($ssm_id);

function firstname($id)
    $f="SELECT firstname FROM register WHERE matri_id='$id'";
    while($row=mysql_fetch_array($rr)) {
        $firstname = $row['firstname'];
    return $firstname;



Without knowing the exact format of your JSON, it's hard to give a definitive answer. However, assuming you have a JSON array of objects that represent your rows, you'd need to iterate over that array and for each object do the following:

Create a new<tr> element -var $tr = $('<tr/>');
For each item of information (I assume one item per<td>), create a<td> element and set its content -var $td = $('<td/>').html(x.y) (x is your current object, y is a field in the object) then append it to the row -$tr.append($td);.
Append the row before the last row in the existing table -$('.list-order tr:last').before($tr);
Following from the additional information provided in the question, the following code should do what you need to do:

success: function(result) {
    //result is json format
    var $tr = $('<tr/>');
    $('.list-order tr:last').before($tr);

People are also looking for solutions to the problem: php - File extension is change upper case to lower case in codeigniter


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.