I am using the ajax() function to send a string to a PHP file.
In case of a success, the PHP file echoes "the validation of the string was successful". But I would also like to send the actual string back to jQuery and put it in a div.
How can send back that string, without having to echo it along with the confirmation message?
Or should I put both messages into a string, send it to jQuery and have jQuery split that string? What's the best way?
Putting what others have said into code, you might set up your PHP script to return a JSON-encoded object like so:
$result = array(
'message' => 'the validation of the string was successful',
'original' => 'string sent by client'
);
echo json_encode($result);
On the client side, you can then access each piece from your callback function like so:
function (result) {
alert(result.message);
// do something with result.original
}
I would return JSON with the string and the validation message.