-1

In my php file

foreach($qry_1 as $v)
{       
    $subcat=array('title'=>$v['title'],'cat_id'=>$v['cat_id']);
    echo json_encode($subcat);
}

in my jquery file

$.ajax({
    type:'POST',
    url:"php/process.php",
    data:{cat_id:cat_id},
    dataType:"json",
    success: function(data){

        //how to loop through data to show in div
    });        
}
})

in my html file

<div id="title">

</div>

i want to show my json data in div so how can i loop through the json data received from php at jquery and how can i show it in my html file

Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55

1 Answers1

1

try changing php to:

foreach($qry_1 as $v)
{   
    // add to subcat array in each iteration    
    $subcat[]=array('title'=>$v['title'],'cat_id'=>$v['cat_id']);

}
//output final array
echo json_encode($subcat);

Then in ajax callback:

$.each(data, function(_, item){
    $('#title').append('<p> Title: ' + item.title + '</p>'); 
});
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • thank you @charlietfl working perfect but when i use it in dropdown button ie. when i change category dropdown button i got the above json array in another dropdown button but when i again change the category the pevious array not change and it shows along with current array kindly guide me how to destroy previous array – priyabrata sen Mar 27 '16 at 17:37
  • 1
    use `empty()` first if you will be changing content – charlietfl Mar 27 '16 at 17:38
  • can u tell me in which i will put empty() it shows error in php @charlietfl – priyabrata sen Mar 27 '16 at 17:43
  • can u tell me in which i will put empty() it shows error in php @charlietfl – priyabrata sen Mar 27 '16 at 17:59