I am trying to find the best way to pull multiple variables from PHP with an AJAX post. Right now, I am using .substr to split an echoed variable from the PHP itself.
Here's my AJAX post (with jQuery)
$.post("scripts/enter/register.php", {username : username, email : email, password : password}, function(data) {
var callback = data.substr(0,1);
var user_session = data.substr(1);
if(callback === "1"){
//Do a registration script
} else {
//Give an error
}
});
and my PHP
if (strlen($hash) >= 20) {
if($user_name){
//Perform SQL queries
echo "1";
echo $cookie;
} else {
echo "0";
}
} else {
echo "0";
}
As you see, in my php, I echo two variables when everything goes well. When there's an error, I only echo one variable. This script works perfectly fine, but I find it clunky and apparently it is unsecure. On a side note, I have come to understand echo is not secure, though no one has told me what to instead.
I have another script that handles login (this last script is register), but I use a different PHP script. Here are the questions:
you have if(callback === "1"){ in your code.
This should be if(callback == "1"){ assuming this may be a number as well as a string
That may solve part of your problem