Jump to content
Larry Ullman's Book Forums

Funny Quark With Ajax Post/Get

Recommended Posts

I was importing some of the Ajax code covered in the book to a completed site. I was testing out my Ajax skills by ajax-ifying a login form. Well, it turns out that if a PHP variable on the ajax-version of the php script is unset(in my case $dbc), the POST request from Ajax doesn't work. I even made sure the ajax-php page only returned the word 'VALID' and it still wouldn't work. Odd as I ran the php script by itself to test it and it never threw an unset variable error.


Took me about 2 hours to figure that out.


Here's the really funny thing: I switched the Ajax request to GET, and it didn't care about the unset $dbc variable.


The real lesson for me was to always, always remember when ajaxifying a modular type site that the original validation script is usually contained in another script which also contains other settings and such. This is not the case when doing ajax as it runs the php script all alone(unless you are really smart and have your ajax and non ajax validation code in the same file). If you have any questions about what I mean here, please reply. I thought this may help out some of us.

Link to comment
Share on other sites

Sorry guys, I tend to type how I think, which is not always linear.


The $dbc PHP variable was not declared/created on the ajax-version of the validation page, it was just referenced/called(in the mysqli real escape string function).


On the 'normal', non-ajax PHP validation script(following the progressive enhancement mindset and was used as the template(with minor alterations) for the ajax version), the script itself was included in another script(index.php), and on THAT page(index.php), it also included a MYSQL PHP script that contained the database connection info, including the $dbc variable.


So, when the Javascript file opened the ajax-version of the validation script via POST, it could not receive back the 'VALID' response text because of the uncreated YET referenced $dbc variable.


The quark I referenced in the subject line of this thread was that when I switched the Javascript to access the ajax-version of the validation script via GET, instead of POST, it worked just fine.


Also, when I ran the ajax-PHP script by itself(as Larry advised doing, which is AWESOME advice btw), still containing the undeclared $dbc variable, it did not throw PHP error for some reason. Usually if you try to use a variable that has never been created, it will throw all kinds of errors, but apparently not when using the mysqli real escape string function. I hope that clears up my findings. If not, maybe I'l just create a quick youtube vid showing what I mean.

Link to comment
Share on other sites

Without seeing actual code, I'm pretty much guessing, but it's fairly safe to say that the type of request made by Javascript--GET or POST--has absolutely no impact on the existence of PHP variables, aside from those in $_GET and $_POST. I hate to put it so bluntly, but the suggestion that GET/POST in Javascript affects the creation of $dbc variable is ludicrous. Please understand that I'm only being so direct because you can't continue to have a false understanding of the problem here.


Further, you really should be using GET or POST based upon the actual act taking place. Requests for information should go through GET; requests for server changes or actions should go through POST.

  • Upvote 1
Link to comment
Share on other sites

I appreciate the bluntness(word?). I created a complete response detailing: when the ONLY variable was either GET or POST, it changed the ajax.responseText. I then looked at my PHP code again. The IF conditional expected a POST variable, so it progressed to the unset $dbc, but when it ran through GET, it completely bypassed it, and thus the error was never there when using GET. I tested this by simply pasting:

$e = mysqli_real_escape_string($dbc, $_POST['email']);

outside the IF conditional below, and sure enough, when the ajax ran through GET, it threw an error(maybe technically an exception since it is OOP lol). Point is, thank you Larry for causing me to take a 3rd look at the code and not pass on mis-information.


echo 'VALID';

// Array for recording errors:
$login_errors = array();
// Validate the email address:
if ($_SERVER['REQUEST_METHOD'] == 'POST' && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$e = mysqli_real_escape_string($dbc, $_POST['email']);
} else {
$login_errors['email'] = 'Please enter a valid email address!';


ajax.open('POST', 'ajax/loginajax3.php', true);
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data = 'email=' + encodeURIComponent(email) + '&pass=' + encodeURIComponent(pass);

Link to comment
Share on other sites


  • Create New...