EDIT: I changed the post method to a get method and everything worked as it should. I’ll investigate further and if I figure out why that is I’ll add the answer. I do use post in other places so we’ll see…
I’ve searched around for answers to this question and haven’t found anything that has helped so far. I have a simple form that uses jquery’s .submit function to serialize the data and post it to a php script via ajax. This all works great on my localhost but once I publish it to the server, the field values won’t post.
Here is the form:
<div id="loginDiv">
<form id="login" class="form-inline pull-right">
<input id="loginVal" name="loginVal" type="password" class="input-small" />
<button type="submit" class="btn btn-success">Log In</button>
</form>
</div>
Here is the submit function and the success callback function:
// ajax function to login to session
$(document).ready(function() {
$("#login").submit(function() {
$.post(
"functions/sessionLogin.php",
$("#login").serialize(),
//{"loginVal":"CHAPMANADMIN"}, (this was just to troubleshoot)
function(json) {
parseReturnedLogin(json);
}
);
return false;
});
});
// login success handler
function parseReturnedLogin(data) {
var obj = jQuery.parseJSON(data);
$("#loginDiv").html("<p class=\"navbar-text pull-right\">Logged in as <span
style=\"color:#0088cc\">"+obj.priveleges+"</span></p>");
}
This is an example of some of the header and the response I get on localhost:
Request Method:POST Status Code:200 OK
Accept:/
X-Requested-With:XMLHttpRequest
FORM DATA: loginVal:CHAPMANADMIN
RESPONSE:
{“user”:”CHAPMANADMIN”,”priveleges”:”Administrator”,”sessid”:”pvigqmcfumlbiu13esiojg1n74″}
This is an example of some of the header and the response I get on the server:
Request Method:POST Status Code:200 OK
Accept:/
X-Requested-With:XMLHttpRequest
FORM DATA: loginVal:CHAPMANADMIN
RESPONSE:
{“user”:””,”priveleges”:””,”sessid”:”o2nf69vc2tdd8hc8n0usas4010″}
The query on the live server is running with an empty value. I have other AJAX applications on this same server that work as they should with a similar setup so I’m not sure what’s happening and would love some advice/troubleshooting direction from the experts!
This ended up being an issue with using mysql_real_escape_string on my $_POST variables. I converted my code from mysql to PDO functions but did not remove the mysql_real_escape_string functions which require a mysql connection to work or else they return false.
When I posted this code to the server there were no active mysql connections so the function returned false and set the value of my variables to false.
Hope this helps someone in the future from the same silly mistake.