I am posting some data using ajax. I want to manipulate that data and return to to the calling jQuery script.
Here is my jQuery:
$.ajax({ type: "POST", url: "somescript.php", datatype: "html", data: dataString, success: function() { //do something; } });
Here is my somescript.php on the server:
<?php //manipulate data $output = some_function(); //function outputs a comma-separated string return $output; ?>
Am I doing this correctly on the server side, and how do I access the return string when the ajax call completes?
You can't return "true" until the ajax requests has not finished because it's asynchron as you mentioned. So the function is leaved before the ajax request has finished.
Create an XMLHttpRequest object. Create the function to be executed when the server response is ready. Send the request off to a PHP file (gethint. php) on the server.
I figured it out. Need to use echo in PHP instead of return.
<?php $output = some_function(); echo $output; ?>
And the jQ:
success: function(data) { doSomething(data); }
It's an argument passed to your success function:
$.ajax({ type: "POST", url: "somescript.php", datatype: "html", data: dataString, success: function(data) { alert(data); } });
The full signature is success(data, textStatus, XMLHttpRequest)
, but you can use just he first argument if it's a simple string coming back. As always, see the docs for a full explanation :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With