EDIT: Okay, now that I think about it, JSON does sound decent for this, and as long as I code right, there shouldn't be a syntax error to disrupt it or anything.
Good evening!
Something that's always bothered me is how to handle returned data from PHP in an AJAX request, and how to even return the data!
Say I have a log in form, and it processes submitted form data through AJAX. We'll say we use GET for simplicity's sake.
"page.php?username=Foo&password=bar"
Here's what bugs/irks me, and why I feel compulsive and feel as though I need to know the right way to do this:
I sent the data and it's been processed. Now what?
Say my username and password are valid/correct. What do I do?
Return true in PHP? A string?
Then what do I do with the JavaScript?
if(ajaxRequest.responseText=="true"){
//Username and password are correct! Execute this...
}
What I thought was a cool idea was returning a JSON object as a string, and parsing through JavaScript.
var object=JSON.parse(ajaxRequest.responseText);
if(object.success=="true"){
//Username and password are correct! Execute this...
}
But, what if my username and password combo is...wrong? Do I return false via PHP, or a string again?
Between true and false, there are only 2 choices: 1. The username and password were correct 2. The username and password were not correct
But what if the username doesn't even exist? Returning "false" doesn't help me tell the client exactly why they can't log in.
Diving deeper into my OCD on this, what about unexpected or parse errors?
If there is some sort of DB connection error or a parse error, how do I return that to tell the user? I looked into Trying...and Catching syntax errors, but I had no luck.
A recent thought I had was to do THIS:
If everything executes properly, and the username and password exist (going back to our original scenario), then we don't return ANYTHING. The responseText will be an empty string. That means the user logged in successfully!
Otherwise, if there is an error, DO return something (typically a string) and display it as an error.
Am I going about this the right way? Am I on the right track?
My personal preference, which is in no way "THE" correct way to do it, is to use JSON for everything.
So I have a JavaScript function that, when a form is submitted, will convert the form data to a JSON string and POST it to the server.
The server then processes it, and returns another JSON object. In your login example, therefore, the server would receive:
{"username":"Foo","password":"bar"}
And it would return one of these:
{"ok":false,"error":"No user with that name was found."}
{"ok":false,"error":"The password you entered was incorrect."}
{"ok":false,"error":"Failed to log you in (error code: ###)"}
{"ok":true,"redirect":"/home"}
Every AJAX request response has this "ok" property. If it is false, there will be an "error" property saying what happened (and there may be additional data depending on the situation). On success, the "ok" property is true. A lot of requests just get {"ok":true}
all by itself, but sometimes there is additional data here too.
As I said, this is not "THE" way to do it, but I like it and it's consistent.
EDIT: Forgot to mention the bit about PHP errors.
The JavaScript that receives the server's response attempts to parse it as JSON using a try...catch
block. If parsing fails, then there was a server error. In such a case, an error message is popped up with the raw response from the server, with a note advising the user to contact me about the error message.