Jack
Jack

Reputation: 9784

Passing whether a checkbox is checked, via jQuery, to PHP

I have an AJAX call in my jQuery/PHP app. It needs to send some data to a PHP script to process (I have this part working fine).

My jQuery variable:

var form_data = {
   urltitle: '<?php echo $urltitle; ?>',
   subscribe: $('#subscribe').val(),
   comment: $('#commentbox').val()
};

This works fine, the PHP script reads the values of all the variables - except for the 'subscribe' variable. Subscribe is a checkbox, like so:

<input type="checkbox" name="subscribe" checked="checked" value="subscribe" id="subscribe"/> 

How can I pass the 'value' of the checkbox to my PHP script? I have a non-AJAX version of this script too, which is almost identical, except on the PHP side I use:

if (isset($_POST['subscribe'])) { /* do something */ }

And that works fine...

Thanks!

Jack

Upvotes: 1

Views: 1569

Answers (2)

darren102
darren102

Reputation: 2830

When you say value do you mean subscribe or do you just want to know if the checkbox has been checked or not.

If you just want to know if it has been checked then you can do $('#subscribe:checked').val() and if it is null then you know it is not checked and if it is not then you should have the value in your case subscribe.

Upvotes: 1

Rob
Rob

Reputation: 1885

.is(':checked') should work

Upvotes: 1

Related Questions