Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-29-2009, 02:29 AM   PM User | #1
Bobafart
Regular Coder

 
Join Date: Dec 2006
Posts: 417
Thanks: 168
Thanked 1 Time in 1 Post
Bobafart is on a distinguished road
JQuery: how to pass var for checkboxes?

I have a form which has a checkbox input field

Code:
<div><input type="checkbox" id="emailSubscription" name="emailSubscription" value="1" <?php if($userActiveDailyEmail==1){ echo 'checked'; } ?> /> daily email reminder of the new case of the day</div>
If the user checks it or unchecks it (toggles it) I want my jquery ajax function to update the mysql table depending on if the box is checked (value = 1) or not (value = 0)

Code:
	<script type="text/javascript">
	$(document).ready(function(){
		$('#emailSubscription').click(function() {
			var emailSubscription = $("#emailSubscription").val();
			alert($("#emailSubscription").val());
			$.ajax({
				method: "get",
				url: "ajax_emailSubscription.php",
				data: "emailSubscription="+emailSubscription
			}); //close $.ajax(
		}); //close click(
	}); //close $(
	</script>
The problem is that no matter if the checkbox is checked or unchecked the alert always defines the value of emailSubscription as 1.

How do I make it so that if the checkbox is unchecked emailSubscription = 0 and if it is checked then emailSubscription=1 ?
Bobafart is offline   Reply With Quote
Old 11-29-2009, 03:22 AM   PM User | #2
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Testing the value is always going to give the value, which you've set to 1. Rather, test for checked. Here's a snippet from a project I'm working on:
Code:
    $('div#regions :checkbox').change(function(){
        if ($(this).is(':checked'))
        {
            // do something
        }
        else
        {
            // do something else
        }
    });
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Users who have thanked tomws for this post:
Bobafart (11-29-2009)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:43 PM.


Advertisement
Log in to turn off these ads.