Php and $ajax - how do you send multiple fields back?

I’m using and ajax call with multiple fields


temp1='v1'; temp2='12'; temp3='Mary'; temp4='2'; temp5='A-'; temp6='unknown';
$.ajax({
    type: 'POST',
    url: 'process.php',
    data: {
		version: temp1,
		sessionnumber: temp2,
		name: temp3,
		numofhours: temp4,
		testresult: temp5,
		gradepointaverage: temp6     
    },
    success: function(_log) {
         $('#readout').append(_log);
    }
});	

and in process.php


$received = '<div>Data received for ' . $name .' '.$testresult. '</div>';
echo $received;

I want to append to $(‘#readout’) a whole new set of data calculated off what was just sent. Such as a gradepointaverage calculated off other scores in mysql for that $name plus a new issued sessionnumber.

Would anyone know how to mod this code so I am sending back multiple fields?
Is it possible to originate an ajax call from the php end at a later point in time?

Send back a JSON object as your response.


$data = array('version' => 'something', 'name' => 'somethingelse');
header("Content-Type: application/javascript");
echo json_encode($data);

Then you’ll have an object in your AJAX callback instead of a string:


    success: function(data) {
         alert(data.name);
         alert(data.version);
    }

What is the benefit of json callback over a javascript one?
I would not use json and just return javascript which the browser parses rather than having to turn the json back in to javascript.

Not sure what you’re talking about. JSON is a JavaScript response. As you can see, the AJAX callback gets a native JavaScript object you can directly access the properties of. You don’t have to do anything to “turn the json back in to javascript”.

I will start testing with a json object – thank you for pointing me in this direction.

Is it possible to initiate the ajax call from the server side?

Strictly speaking what is passed back is a string representation of an object - to convert it into an actual object you would call JSON.parse() on the string (which reverses what the json_encode call did in the PHP).

Can you put that another way?

It doesn’t make sense to say “initiate the ajax call from the server side”, since an AJAX call is by definition initiated from JavaScript, which runs on the client.

A situation where the server sends simple data or complex data (like json object) intermittently, the client side does not control the timing.
What technology would this be ?

You do that through “long polling” or “Comet”. The connection is still initiated by the client (via AJAX or websockets), but it sits open until the server responds – intermittently, not immediately, only when there’s something to send. When the client receives a response, it immediately starts a new connection to wait for the next message.

If you intend to have a lot of users online, you’ll need a different web server for this. The standard Apache or IIS setup won’t be able to accomodate holding all those open connections very well.

Got it! I was asking for my own curiosity and learning more than anything. My application will be just an ajax call with
a json object passed back as you suggest.
Thanks Dan

the server can respond with pure javascript, and a setTimeout or setInterval in pure javascript. So yes, the server can initiate intermittent responses.

I would say this is the best way. You do not leave a connection open, and if your site starts to bottleneck you won’t be adding to the problem by creating more client-side calls