eatonphil
eatonphil

Reputation: 13672

AJAX jQuery PHP Return Value

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?

Upvotes: 8

Views: 32501

Answers (4)

woz
woz

Reputation: 10994

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.

Upvotes: 7

Teena Thomas
Teena Thomas

Reputation: 5239

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

Upvotes: -1

Gnietschow
Gnietschow

Reputation: 3180

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

Upvotes: 15

Samuel Cook
Samuel Cook

Reputation: 16828

<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.

Upvotes: 3

Related Questions