Posting Checkbox Values Through JQuery Into PHP
I've created a user login form with a "Keep Me Logged In" checkbox and I'm trying to pass the value through JQuery to a PHP file that will process the information but I'm unsure of the correct code to use. Here is what I've got so far:
var username=$("#username").val();
var password=$("#password").val();
var checkbox=$("#checkbox").val();
if(usernameok == true && passwordok == true)
{
$('.validation').html("Logging In").removeClass("error").addClass("success");
jQuery.post("php/login.php", {
username:username,
password: password,
checked: checked
}, function(data, textStatus){
if(data == 1){
window.location.replace("home.php");
}
else{
$('.validation').html("Wrong Password Given").removeClass("success").addClass("error");
}
});
}
Then in the Login PHP page I'm using Request as follows:
$username= mysql_real_escape_string($_REQUEST["username"]);
$password= md5(mysql_real_escape_string($_REQUEST["password"]));
$checkbox= mysql_real_escape_string($_REQUEST["checkbox"]);
The username and password values are fine but I'm having trouble passing the checkbox. I understand that there should probably be a :checked statement somewhere in there but I'm not sure where to put it.
Thanks
Answer
Solution:
this is shorthand if statement, or if you prefer regular if statement:
Answer
Solution:
You need to check if the checkbox is checked or not (true/false), and not get it's value. Then use the correct variable in the ajax function to pass it :
On the other hand, why use ajax if all you intend to do is to redirect anyway, which you could do a lot easier with a regular form submit ?