Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending information back and forth with AJAX

With $.post, you can send information to the server, but what when you need to receive information from the server?

How does information change from being of a way that can be held by a php variable to being of a way that can be held by a javascript variable and vice-versa?

like image 854
Delirium tremens Avatar asked Dec 29 '22 21:12

Delirium tremens


1 Answers

This is more relevant to your question: http://docs.jquery.com/Ajax/jQuery.post

Alert out the results from requesting test.php (HTML or XML, depending on what was returned).

$.post("test.php", function(data){
  alert("Data Loaded: " + data);
});

Alert out the results from requesting test.php with an additional payload of data (HTML or XML, depending on what was returned).

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

Gets the test.php page content, store it in a XMLHttpResponse object and applies the process() JavaScript function.

$.post("test.php", { name: "John", time: "2pm" },
  function(data){
    process(data);
  }, "xml");

Gets the test.php page contents which has been returned in json format ("John","time"=>"2pm")); ?>)

$.post("test.php", { func: "getNameAndTime" },
  function(data){
    alert(data.name); // John
    console.log(data.time); //  2pm
  }, "json");
like image 197
Shadi Almosri Avatar answered Jan 05 '23 10:01

Shadi Almosri