Hi all,
I'm having some problems both sending information to the backend (PHP) using POST, as well as successfully getting a response (if and when sending works).
I should preface everything by specifying that I'm working entirely in a local environment (using MAMP), which my research thus far indicates could be a contributing factor (?). Should also mention, along those lines, that I've modified my response script to accept status 0 as well as 200 (the former being an acceptable response when running in a local environment).
I've gotten to the point where I get the appropriate readyState and status responses, but there are issues in actually communicating with the PHP it seems.
First of all, here are my JS functions:
Code:
function getXMLHTTPRequest() {
var req = false;
try {
/* for Firefox */
req = new XMLHttpRequest();
} catch (err) {
try {
/* some IE versions*/
req = new ActiveXObject("Msxm12.XMLHTTP");
} catch (err) {
try {
/* for some other versions of IE */
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (err) {
req = false;
}
}
}
return req;
}
function validateLogin() {
var email=document.forms["signIn"]["email"].value;
var pass=document.forms["signIn"]["password"].value;
//alert(email + pass);
if (email==null || email=="" || pass==null || pass=="") {
$("#invalid").addClass("hidden");
$("#emptyField").removeClass("hidden");
return false;
}
var params = "email="+encodeURI(email)+"&pass="+encodeURI(sha1(pass));
var url = "validate_user.php";
//alert(params);
var req = getXMLHTTPRequest();
req.open("POST", url, true);
req.onreadystatechange = function() {
checkResponse(req);
};
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.setRequestHeader("Content-length",params.length);
req.setRequestHeader("Connection", "close");
req.send(params);
}
function checkResponse(req) {
if (req.readyState == 4) {
if (req.status == 200 || req.status == 0) {
//var result = req.responseText;
alert("Server says: "+ req.responseText);
} else {
alert("There was a problem with the request: " + req.status + req.statusText);
}
}
}
Instead of the "validate_user.php" destination indicated above, I first tried a simpler "hello_ajax.php" test:
Code:
<?php
require_once('fns.php');
$conn = db_connect();
$new_id = rand();
$pass = mysql_real_escape_string($_POST['pass']);
$result = $conn->query("insert into test values
('".$pass."', '".$new_id."')");
echo "Hello Ajax caller!";
?>
With this, I was able to check that entries were successfully added to the "test" table. Also, the "pass" value being passed in was correct (from my html form that calls the ajax).
However, the echo'd message was not successfully received back in the "checkResponse" JS function - the alert was simply "Server says: " with no response text. That illustrated the receiving problem.
Now, consider "validate_user.php" instead of the hello_ajax function before:
Code:
<?php
// include function files for this application
require_once('fns.php');
session_start();
//create short variable names
$email = mysql_real_escape_string($_POST['email']);
$pass = mysql_real_escape_string($_POST['pass']);
$conn = db_connect();
$result2 = $conn->query("insert into test values
('".$email."', '".111."')");
if(login2($email, $pass, 'USER')) {
// if they are in the database register the user id
$_SESSION['valid_user'] = $email;
echo "true";
//return true;
} else {
echo "false";
//return false;
}
With this, the database query at the start of the function is simply so I can check that we make it to that point - but we don't. In this case, it seems like we're not even getting into the PHP. I tried comparing this with the hello_ajax example but can't see any differences that would lead to this apparent breakdown in communication.
If anyone could help me out with any or all of these issues, I would appreciate it immensely!