I followed the tutorial from this page:
http://www.keyboardninja.eu/webdevelopment/jquery-ajax-call-tutorial
and already got the random number output. But how can I change the function (and the php file?), that I get multiple returns ("msg" in the example), to fill different divs? Or is that not possible, so I gotta fill the containing div with the stuff and put a whole lot of code into the echo part in the php file?
Thanks
in process.php file, add all messages in array than convert into json , as given below.
$msg=array();
$msg[] = $msg1;
$msg[] = $msg2;
.
.
........
echo json_encode($msg);
now process json in your ajax call to show all of your message using each in javascript
Try this,
In your ajax file, append the multiple messages with '|' or '~' and when it gets return , split it in JS; and place it in your DIVs through innerHTML.
ex (in ajax)
echo $msg1.'|'.$msg2.'|'.$msg3;
Simple do this on Php side (ajax.php):
$arr = array();
$arr['msg1'] = '<p>Hi I am some random ' . rand() .' output from the server First Msg.</p>';
$arr['msg2'] = '<p>Hi I am some random ' . rand() .' output from the server Second Msg.</p>';
$arr['msg3'] = '<p>Hi I am some random ' . rand() .' output from the server Third Msg.</p>';
echo json_encode($arr);
And on Js side:
function myCall() {
var request = $.ajax({
url: "ajax.php",
type: "GET",
dataType: "json"
});
request.done(function(data) {
$("#mybox1").html(data.msg1);
$("#mybox2").html(data.msg2);
$("#mybox3").html(data.msg3);
});
request.fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}