0

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;
?>
0

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

5 Comments

Make sure to add that you need dataType: 'json' in your AJAX call, and that array_from_php is now an object.
@Rocket the docs say: "dataType - Default: Intelligent Guess (xml, json, script, or html)" api.jquery.com/jQuery.ajax
That's only if you output your PHP as Content-type: application/json. Otherwise, jQuery treats it as text.
@Rocket Are you sure about that? Don't get me wrong though, dataType: 'json' is def best practice with the correct PHP Content-type headers.
Yes. If you don't add header('Content-type: application/json'), jQuery "intelligently guesses" that it's text.
3

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]);

        }
        })   
});

3 Comments

It's not just gonna magically be an array. You need to have PHP output it as such (json_encode).
Wouldn't array_from_php.name be better than array_from_php[0]?
thanx every 1 and stackoverflow.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.