2

I have a simple checkbox on a page that allows a user to say if they'd like to receive email notifications. I am using jquery for this to call some php code when the checkbox changes. However, I am not having much luck even calling the jquery function (clicking the checkbox does nothing) let alone test the backend functionality.

Any help in pointing out the error would be great. Thanks.

The checkbox HTML:

<input id="notify_checkbox" type="checkbox" value="y" name="notify">

The jquery:

$('#notify_checkbox').change(function(){

    if($('#notify_checkbox').attr('checked'))
    {
        $.post("/update_notify", { checked: "y", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Awesome, we'll send you an email!</p>" );
    }
    else
    {
        $.post("/update_notify", { checked: "n", email: "<?php echo $this->session->userdata('email');?>" });
        $( "#notifyresult" ).html( "<p>Okay, we won't email you.</p>" );
    }
});

And finally the PHP:

function update_notify()
{
    // Passed through AJAX
    $notify = $_POST[checked];
    $email = $_POST[email];

    $this->load->model('musers');
    $query = $this->musers->update_user_notify($email, $notify);
}

RESOLUTION: The comments below were helpful but not the ultimate solution. The solution was to add the following around my code.

$(document).ready(function() {
{);
1
  • 3
    In your PHP, you should quote your array keys: $_POST['checked'] and $_POST['email']. Commented May 19, 2011 at 19:38

3 Answers 3

3

Why not use .click() instead?

JSFIDDLE

Also, as you can see in my JSFiddle example, use .is(':checked') instead of attr('checked').

edit after @Rocket commented on your post:

You should indeed quote your $_POST values in your php! Didn't notice it myself, credits to rocket

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I see this works in your jsFiddle code, but for the life of me I can't figure out why clikcing on my checkbox (with the .is(':checked') changes) nothing happens! I even put an alert like you jsFiddle, but still clicking the checkbox does nothing. Grrr.
Might be a silly question, but does it matter if the jquery function is not in between the <head></head tags? I have it in the body of the view, not the header.
0

What's the name of your controller? You need to put that in the URL.

 $.post("/controller/update_notify", ...

1 Comment

Correct, I actually had this in my code but removed it from the sample paster here for brevity.
0

The problem is with the redefinition of the attr function in jQuery 1.6, and with the difference between attributes and properties.

With attributes (retrieved with attr), the value of checked="checked" or its absence stays the same, regardless of whether the element is actually checked or not.

With properties (retrieved with prop as of jQuery 1.6), the actual state of the element is found. This is equivalent to checking the checked property of the element (which is preferable because you don't need to do a new jQuery selection). The best soltion would be as follows:

if (this.checked) {

See jsFiddles showing this:

Comments

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.