php - set json data array in function response -
here ajax function in response getting result don't know how set response in page. possible json_decode or have try else
json file is
<?php $group_id = $_post['group_id']; $query = "select *,group_id contact join addressgroup on addressgroup.contact_id = contact.contact_id group_id in (".$group_id.") group contact.contact_id"; $res = mysql_query($query); $data = array(); $k=0; while($row = mysql_fetch_array($res)) { $data[$k][0] = $row['user_id']; $data[$k][1] = $row['first_name']; $data[$k][2] = $row['middle_name']; $data[$k][3] = $row['last_name']; $k++; } echo json_encode(array($data)); ?>
ajax function
var myarray; function getcon() { myarray = []; myarray.push($(".group_id").val()); $.ajax({ datatype: "json", type: "post", url: "getcon.php", data: 'group_id=' + myarray.join(), success: function(data) { totalrecords=data.length; zone.fncleartable(); for(var i=0; < (data.length); i++) { zone.fnadddata([ data[k][0], data[k][1], data[k][2], data[k][3], ]); } return false; } }); return false; }
as response json object.. have use this:-
replace
echo json_encode(array($data));
by
echo json_encode($data); /* because $data array*/
and keep same earlier
while($row = mysql_fetch_array($res)) { $data[] = $row['user_id']; $data[] = $row['first_name']; $data[] = $row['middle_name']; $data[] = $row['last_name']; $k++; } <script> var myarray; function getcon() { myarray = []; myarray.push($(".group_id").val()); $.ajax({ datatype: "json", type: "post", url: "getcon.php", data: 'group_id=' + myarray.join(), success: function(data) { console.log(data)/* check structure of data here*/ zone.fncleartable(); zone.fnadddata([ data.user_id., data.first_name, data.middle_name, data.last_name, ]); return false; } }); return false; } </script>
Comments
Post a Comment