0

I'm using the following ajax:

       $.ajax({ 
                 type: 'POST', 
                 url: '/search/search.php', 
                 crossDomain: true,
                 data:  {data: data},
                 dataType: 'json', 
                 async: false,

                 success: function (response){ 
                    if (response.success) 
                        {
                           $('#search-results').show();

                           for(field in response.data){

                           error = response.field_errors[field];           

                               var name = field.name;
                               var barcode = field.barcode;
                               var serial = field.serial;

                               $("#searchname").html(name);
                               $("#searchbarcode").html(barcode);
                               $("#searchserial").html(serial);


                           }
                        } 
                    else {
                        console.log("fail");
                    }
                 },

               }); 

I'm trying to loop through the rows returned from the php, and put each row as a row in the table of my html.. I get the correct response from the php, but my loop doesn't show anything in the html.

HTML Table

        <table class="table" id="search-results" style="display:none;">
          <thead>
            <tr>
              <th>#</th>
              <th>Name</th>
              <th>Serial</th>
              <th>Barcode</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>1</td>
              <td id="searchname"></td>
              <td id="searchbarcode"></td>
              <td id="searchserial"></td>
            </tr>
          </tbody>
        </table>

PHP

    $json = array();
    if($num > 0)
    {

        $json['success'] = TRUE;
        $i = 0;
 while ($row = mysql_fetch_array($sql))
 {
    $json[$i]['data']['name'] = $row['name'];
    $json[$i]['data']['barcode'] = $row['barcode'];
    $json[$i]['data']['serial'] = $row['serial'];

    $i++;
}
    }
    else
    {
        $json['success'] = FALSE;
    }

    echo json_encode($json);
9
  • What is wrong ? What do you expect it to do ? What does it do instead ? Please ask a real question. Commented Apr 17, 2014 at 14:39
  • I'm trying to add a row to my html table for each row in the php response. It currently doesn't show anything on the html but gives me the correct json response from the php Commented Apr 17, 2014 at 14:40
  • 1
    Why are you using a loop? As ID's are unique, you will only overwrite your values with each row. And what does console.log(field); give you in the loop? Commented Apr 17, 2014 at 14:45
  • I don't want to overwrite my values, I've updated my question with the code of my table. is there a way to generate html for each row? Commented Apr 17, 2014 at 14:46
  • 1
    Also var name = field.name; in for..in doesn't make sense. You probably need var name = response.data[field].name;. And the same for barcode and serial. Commented Apr 17, 2014 at 14:49

1 Answer 1

3

You can use jquery .each(). to itterate the array, and jquerys .append() to add the table rows:

If the data is an array of objects:

$.each(response.data, function( index, item ) {
    $('#search-results').append('<tr><td>' + item.name + '</td><td>' + item.barcode + '</td><td>' + item.serial + '</td><td></tr>');
});

If its an array of arrays:

$.each(response.data, function( index, item ) {
    $('#search-results').append('<tr><td>' + item['name'] + '</td><td>' + item['barcode'] + '</td><td>' + item['serial'] + '</td><td></tr>');
});

https://api.jquery.com/jQuery.each/

EDIT you php creates odd json, hence your problems. Fix it up:

$json = array();
if($num > 0)
{

    $json['success'] = TRUE;

    while ($row = mysql_fetch_array($sql))
    {
          $json['data'][]=array('name'=>$row['name'], 'barcode'=>$row['barcode'], 'serial'=>$row['serial']);
    }
}
else
{
    $json['success'] = FALSE;
}

echo json_encode($json);
Sign up to request clarification or add additional context in comments.

6 Comments

I get TypeError: object is undefined length = object.length
@user1738017 please edit your question to show me the contents of response.data, eg with console.log(response.data); or just show the php code that creates it in the 1st place.
the full response is {"success":true,"0":{"data":{"name":"carla","barcode":null,"serial":"test"}},"1":{"data":{"name":"test","barcode":null,"serial":"test"}}}
@user1738017 your json is messed up. I have edited my answer to fix it for you.
I've changed it but now I see this error ` <br /> <b>Notice</b>: Use of undefined constant serial - assumed 'serial' in <b>/Applications/XAMPP/xamppfiles/htdocs/search/search.php</b> on line <b>22</b><br /> <br /> <b>Notice</b>: Use of undefined constant serial - assumed 'serial' in <b>/Applications/XAMPP/xamppfiles/htdocs/search/search.php</b> on line <b>22</b><br /> {"success":true,"data":[{"name":"carla","barcode":null,"serial":"test"},{"name":"test","barcode":null,"serial":"test"}]}`
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.