I have an AJAX call on txt_RollNo
's blur
event to bring std_name
,Class
,age
from database and fill it in txt_name
,txt_class
,txt_age
.
In a single call can I have name
,class
,age
all as a whole in array or any, how to seperate it.
$("#txt_RollNo").blur(function(){
$.ajax({
url:'getSudent.php',
data:'stdID='+ $(this).val().trim(),
success:function(array_from_php)
{
//but here i m receiving php array, how deal in jquery
//$("#txt_name").val(array_from_php);
//$("#txt_Class").val(array_from_php);
//$("#txt_age").val(array_from_php);
}
})
});
getSudent.php echos array as below
<?php
$qry=mysql_query("select * from students where studentID='".$_GET['std_ID']."'");
$row=mysql_fetch_array($qry);
echo $row;
?>
PHP:
<?php
header('Content-type: application/json');
$qry=mysql_query("select * from v_shop where shop_no='".$_GET['shopno']."'");
$row=mysql_fetch_array($qry);
echo json_encode($row);
?>
JavaScript
...
$.ajax({
dataType: 'json',
...
success:function(array_from_php){
console.log(array_from_php); // WTF is this?
See json_encode : http://php.net/manual/en/function.json-encode.php
First in php send it as json:
echo json_encode($row);
then just treat it as any array:
$("#txt_RollNo").blur(function(){
$.ajax({
url:'getSudent.php',
data:'stdID='+ $(this).val().trim(),
dataType : 'json',
success:function(array_from_php)
{
// i suggest you output the array to console so you can see it
// more crearly
console.log(array_from_php);
$("#txt_name").val(array_from_php[0]);
$("#txt_Class").val(array_from_php[1]);
$("#txt_age").val(array_from_php[2]);
}
})
});