Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getJSON with jquery

Tags:

json

jquery

php

When i try to display the data with getJSON nothing happens, $('#child-left-container-2') should display data from the php file. Where did i go wrong?... Below is brief example of my code.

php file

while($row = mysql_fetch_assoc($query)) {

//code

    $array[] = "<div class='array-container-2' $style id='$id' data-sid='$sid' data-user='$user'>$details1</div>";


            }

            echo json_encode($array);

jquery

$(function() {
    $('.next_button').click(function() {

var id =  $('#container').find('.graphic-blank:visible').siblings().attr('id');

        $.getJSON('fetchstack.php?id='+id,function(data) {

            $('#child-left-container-2').html(''); //clear div

            $.each(data,function(i,result) {
                $('#child-left-container-2').append(result);
            });
        });

          });
});
like image 590
user892134 Avatar asked Nov 13 '22 11:11

user892134


1 Answers

I dont't know if this would solve the problem you are experiencing but this would be how I would solve this particular problem. Give this code a try, if it works, stopping sending html thru json would be a bonus.

$key = 0;
while($row = mysql_fetch_assoc($query)) {
    //code
    $array[$key]['id']      = $id;
    $array[$key]['sid']     = $sid;
    $array[$key]['user']    = $user;
    $array[$key]['content'] = $details1;
    $array[$key]['style']   = $style;
    $key++;
}

echo json_encode($array);

JS:

$(function() {
    $('.next_button').click(function() {

        var id =  $('#container').find('.graphic-blank:visible').siblings().attr('id');
        $.getJSON('fetchstack.php?id='+id,function(data) {
            $('#child-left-container-2').html(''); //clear div

            for (var i in data){
                var id      = data[i].id;
                var sid     = data[i].sid;
                var user    = data[i].user;
                var content = data[i].content;
                var style   = data[i].style;
                $('#child-left-container-2').append($('<div />').addClass('array-container-2')attr('id', id)
               .attr('data-sid', sid).attr('data-user', user).html(content);
            }           
       });
   });
});

I confess that not knowing what $style is or how it would appear rendered I couldn't add it to the chain. If you post the content of $style I can update the anwser. I hope this helps.

like image 84
petervaz Avatar answered Nov 16 '22 02:11

petervaz