8

I am new to AJAX and am kind of confused by what PHP passes back to the jQuery. So you have an AJAX function like this:

 $.ajax({ url: '/my/site',
     data: {action: 'test'},
     type: 'post',
     success: function(output) {
                  alert(output);
              }
 });

(I took this from ajax another StackOverflow page.)

But on various other resources they will have the success section look like this:

 success: function(data) {functionfoocommandshere}

I am just confused as to what dictates the naming of this variable? If the PHP ultimately echoes an array:

  echo $myVar;

How can I get this from the AJAX?

6
  • 1
    The data variable of the success method will hold anything you echo in PHP. You can not pass an array directly; you have to convert it to JSON first. Commented Nov 13, 2012 at 18:08
  • Which variable are you confused about the naming of? Commented Nov 13, 2012 at 18:09
  • See this php4every1.com/tutorials/jquery-ajax-tutorial Commented Nov 13, 2012 at 18:09
  • The variable that contains the returning php information. So it just doesn't matter? the .ajax command will know that anything within the function () arguments is returning php information? Commented Nov 13, 2012 at 18:10
  • Also: you can'y echo an array in PHP, you can print_r or var_dump or serialize it, but echo is meant for strings or ints, etc. Commented Nov 13, 2012 at 18:10

4 Answers 4

15

An Ajax-Requests fetches a whole site. So you'll not get any data in variables, but the whole site in the data-parameter. All echos you made together will be in this parameter. If you want to retrieve an array, you should transform it to json before.

echo json_encode($myArray);

Then you can receive it via Ajax in this way

$.ajax({ url: '/my/site',
 data: {action: 'test'},
 dataType: 'json',
 type: 'post',
 success: function(output) {
              alert(output);
          }
 });
Sign up to request clarification or add additional context in comments.

2 Comments

Do you really need to echo the value? what about if you don't want to show the output? @Gnietschow
echo does not show anything to the user it only writes data in the response of the server, so it is sent to the client. After the ajax call received the output, It is up to you whether you show this response to the user or not.
7

In you PHP file, use json_encode to turn the array into a more convenient format for use in Javascript. So you would have something like:

echo json_encode($myArray);

Then, in your JavaScript, the data variable of the success method will hold the JSON. Use jQuery's parseJSON to convert this to a JavaScript object, which will then be very easy to manipulate. I don't know what you array contains, but you might do something like this:

$.ajax({ url: '/my/site',
    data: {action: 'test'},
    type: 'post',
    success: function(data) {
        var obj = jQuery.parseJSON(data);
        alert(obj.name[0] === "John");
      }
});

Again, the data variable here will contain anything your PHP outputs, but JSON is a common and convenient way to transfer data back to your JavaScript.

Comments

3
<script type="text/javascript">
$.ajax({
    url: '/my/site',
    data: {action: 'test'},
    type: 'post',
    success: function(output) {
        alert(output);
    }
 });
</script>

<?php
$action = $_POST['action'];
echo $action;?>

Any output that is printed/echoed will be returned to the success function. This is handy when you want to fill an html container with something that you need to run in real time.

Once you get the hang of this, another option is to use JSON to return variables with values.

1 Comment

its retrieving the complete html formate, i want to return only the value
-1

The data that's returned from the PHP AJAX function, can be retrieved from the success block. Here's the manual

 $.ajax({ url: '/my/site',
 data: {action: 'test'},
 type: 'post',
 dataType: 'json',
 success: function(output) {
     //output is the data returned from PHP AJAX function in JSON format
     alert(output); 
   }
 });

3 Comments

So it just doesn't matter? the .ajax command will know that anything within the function () arguments is returning php information?
It's not a matter of "knowing", it's part of the description of .ajax: the success function receives the returned data as an argument.
-1 You're not automatically guaranteed that your ajax call will know that passed JSON is actually json.....and Jquery isn't dealing with PHP, it's dealing with data (remember, PHP is interpreted before it outputs to screen, where JQuery has access to the data) By default, it'll try its best guess, which isn't guaranteed to be JSON. That's why best practice is to define the dataType (could be json, text, jsonp, html, etc) and then to expect that as the return in the success function.

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.