1

I have a checkbox as such

<input name="notify" type="checkbox" id="notify" class="notify" />

I am using an ajax call as such

$("form#submit").submit(function() {
// we want to store the values from the form input box, then send via ajax below
var username = $('#username').attr('value');
var email = $('#email').attr('value');
var password = $('#password').attr('value');
var cpassword = $('#cpassword').attr('value');
var notify = $('#notify').attr('value');
var session_user_id = $('#session_user_id').attr('value');
$.ajax({
type: "POST",
url: "../../includes/editprofile.php?",
data: "username="+ username+
    "&email="+ email+
    "&password="+ password+
    "&cpassword="+ cpassword+
    "&notify="+ notify+
    "&session_user_id="+ session_user_id,
success: function(data){
$('div.success').fadeIn();
$('div.success').html(data);
}
});
return false;
});

In my editprofile.php file I am simply echo'n $notify to see what the value is after I submit the form and no matter what its always "on" whether I check the box or not. Any clues?

1
  • have to use post ....like echo $notifi=$_POST['notify']; Commented Apr 10, 2013 at 5:47

3 Answers 3

3

all 3 should work below

try

$('#notify').is(':checked');

or

$('#notify').attr('checked');

or

$('#notify').prop('checked'); 
Sign up to request clarification or add additional context in comments.

Comments

2

change this

var notify = $('#notify').attr('value');

to

var notify = $('#notify').attr('checked');

Because checkbox does not have value

1 Comment

Just did and it always returns "undefined"
0

Try this

$('#notify').attr('checked') ? "True" : "False";

3 Comments

Thanks this worked, I will check it as answer as soon as I can
I dont think there is any need to write ? "True" : "False", because it'll definitely return either true or false, isn't it ?
@Deepanshu- yes off-course, but we can set value for it instead of true/false. I just made single line code that's it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.