Posting Checkbox Values Through JQuery Into PHP

797

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

741

Answer

Solution:

var checkbox = ( $("#checkbox").is(":checked") ) ? "checked" : "not checked";

this is shorthand if statement, or if you prefer regular if statement:

if( $("#checkbox").is(":checked") ){
    var checkbox = "checked";
} else {
    var checkbox = "not checked";
}
788

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 :

var username = $("#username").val();
var password = $("#password").val();
var checkbox = $("#checkbox").is(':checked');

if(usernameok == true && passwordok == true)
{           
    $('.validation').html("Logging In").removeClass("error").addClass("success");
    jQuery.post("php/login.php", {
        username:username,
        password: password,
        checked: checkbox
    },  function(data, textStatus){
        if(data == 1){
            window.location.replace("home.php");
        }else{
            $('.validation').html("Wrong Password Given").removeClass("success").addClass("error");
        }
    });
}

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 ?

People are also looking for solutions to the problem: php - Forward Users to Custom Dashboard in Wordpress

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.