Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    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 ?

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    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?

  • Users who have thanked tomws for this post:

    Bobafart (11-29-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •