heysamhey
heysamhey

Reputation: 528

jQuery handle php return

I want to have a jQuery script that recieves the return of an php script that made a database request with $.post("script.php", args) the request is started, but how to handle any answer?

Upvotes: 0

Views: 147

Answers (3)

Dhiraj
Dhiraj

Reputation: 33618

You should really read these tutorials before jumping into something

Answer to your question

jQuery post takes parameters like below

jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )

A typical jquery post request with certain parameters, expecting the results from a php file(test.php in this case) in XML format would be like the below

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

Hope this helps.

Upvotes: 3

Nazariy
Nazariy

Reputation: 6088

As many people suggested check with jQuery manual on possible usage of POST or AJAX calls, here is my example of ajax calls:

$.post("server.php", { "func": "getNameAndTime" }, function(data){
  console.log(data.name); //  John Doe
  console.log(data.time); //  2001-01-01 23:00:00
}, "json");

server.php

//do some processing and validation
$response = array('name' => 'John Doe', 'time'=> '2001-01-01 23:00:00');
print json_encode($response);
exit;

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

$.post("script.php", args, function(res) {
  // res is the response send to your from server
  // now do your stuff
});

Read about ajax and post methods and hope you will find solutions

Upvotes: 1

Related Questions