When process.php responds to an Ajax request made by form.html it replies with "false" or "true [hash value]" (where hash value is the output of a hashing function). In form.html I want to call a different function for the two possible responses but how do I parse the response? For example must I call
var responses = xmlhttp.responseText.split(" ")
Assuming the hashing function never outputs "false" I could use
if(xmlhttp.responseText != "false")
Both these ways seem kind of hacky and inefficent, is there a better way?
When process.php responds to an Ajax request made by form.html it replies with "false" or "true [hash value]" (where hash value is the output of a hashing function). In form.html I want to call a different function for the two possible responses but how do I parse the response? For example must I call
var responses = xmlhttp.responseText.split(" ")
Assuming the hashing function never outputs "false" I could use
if(xmlhttp.responseText != "false")
Both these ways seem kind of hacky and inefficent, is there a better way?
-
use
!==
instead of!=
– Janus Troelsen Commented Apr 1, 2013 at 17:54 - Can you change output from process.php? If yes, you can return JSON and use it later in your function. – dikirill Commented Apr 1, 2013 at 18:10
- @dikirill yes I can change the output. I don't know JSON. Could you explain why it would be useful to learn in this case? – Celeritas Commented Apr 1, 2013 at 18:16
- So, you can easily split your logic (true/false) and also send the data (hash) back to JS function. Steffen showed you an jQuery example below. Take a look into JSON.parse() function. – dikirill Commented Apr 1, 2013 at 18:38
2 Answers
Reset to default 4You could do the following in your PHP Code:
$returnValue['ValueA'] = "a value";
$returnValue['ValueB'] = "another value";
echo json_encode($returnValue);
in your JavaScript Code (JQuery is used here):
$.ajax({
type: "GET",
dataType: "json",
url: "./myphpfile.php",
data: "parameter=parametervalue",
success: function(data){
printresult(data);
}
});
function printresult(data)
{
alert(data['ValueA']);
alert(data['ValueB']);
}
Is this helping you?
I've had a similar situation, here is my solution using basic Javascript.
First on the PHP side, I can have one of four outes (PASS or FAIL on an INSert or UPDate), so my response to AJAX carries those outes upfront:
[...]
$echoStr = 'PASS/INS/Adding ID Succeeded.'; // INSert successful
[...]
$echoStr = 'FAIL/INS/Adding ID Failed'; // INSert failed
[...]
$echoStr = 'PASS/UPD/Updating D Succeeded.'; // UPDate successful
[...]
$echoStr = 'FAIL/UPD/Updating ID Failed'; // UPDate failed
[...]
echo $echoStr; return; // Reply to AJAX request.
On the Javascript side (ajax1 is my AJAX obj), I split the response string into three ponents and process accordingly:
[...]
ajax1.onreadystatechange = function() {
if (ajax1.readyState == 4 && ajax1.status == 200) {
response = ajax1.responseText; // PASS or FAIL, INS or UPD, free form text alert
passFail = response.substr(0,4); // PASS or FAIL
insUPD = response.substr(5,3); // INS or UPD
usrMsg = response.substr(9); // Free form alert text
if (passFail == 'PASS' && insUPD == 'INS') {
// do what you need to do here
}
alert(usrMsg);
} // if (ajax1.readyState == 4 && ajax1.status == 200) {
} // ajax1.onreadystatechange = function() {
[...]