2

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

2
  • put your js code here , so let me check Commented Dec 1, 2015 at 10:32
  • it is exactly the same as in the link: function myCall() { var request = $.ajax({ url: "ajax.php", type: "GET", dataType: "html" }); request.done(function(msg) { $("#mybox").html(msg); }); request.fail(function(jqXHR, textStatus) { alert( "Request failed: " + textStatus ); }); } Commented Dec 1, 2015 at 10:34

3 Answers 3

3

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

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

Comments

1

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;

Comments

0

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

Comments

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.