php json data return jquery -
looking best solution:
$.getjson("insertdata.php", {fullname:val1, course_id:course_id, occupation:val2}, function(data) { $.each(data, function(i, user) { //alert(user.arya.status); if(user.arya.status == 'true'){ currentposition = 2; checkdata(); nextslide(); }else{ nextslide(); } }); })
here php code:
mysql_select_db("db", $con); $query="select * table fullname='".$fullname."' , course_id='".$cid."'"; $result = mysql_query($query); $totalrecords = mysql_num_rows($result); if($totalrecords) { while ($row = mysql_fetch_array($result)) { $returndata[]=array( //for json data array 'username' => $row['fullname'], 'arya' => array( 'status' => $row['status'] ) ); } } if(!$totalrecords) { $insertquery="insert table (fullname,course_id,occupation) values ('".addslashes($fullname)."','".addslashes($cid)."','".addslashes($d3)."')"; $result1 = mysql_query($insertquery); }else{ if($stat == "true"){$value = 1;} } mysql_close($con); echo json_encode($returndata);
so in first case when hit php through jquery saves data in database give me error or length. because $returndata empty. there way if $totalrecords false, how send json_encode there no data or value through json_encode jquery function.
thanks in advance.
$returndata = array(); //add $totalrecords = mysql_num_rows($result); if($totalrecords) { while ($row = mysql_fetch_array($result)) { $returndata[]=array( //for json data array 'username' => $row['fullname'], 'arya' => array( 'status' => $row['status'] ) ); } } else { $returndata[] = 'no record'; //add }