Sending Checkbox Status

Michelle Jackson
Michelle Jackson used Ask the Experts™
on
Trying to send checkbox state to PHP page using Ajax. I am able to send he value of the element but I also need to send the state of the button that will determine if the data is added or deleted from the database. Here is what I have.
<script>

 $(document).on('click', '.favorite-resource-checkbox', function(){  
            var resourceValues = $( this ).val();
		    var user = "<?php echo $user_id; ?>";
	 		var id = $(this).attr('id');
           
           $.ajax({  
                url:"file.php",  
                method:"POST",  
                data:{value: resourceValues , user: user},  
                dataType:"text",  
                success:function(data)  
                {  
                     alert(data);  
                    
                }  
           })  
      });		
	
</script>	

Open in new window


Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
You can use the following method:

var checked = ( $(this).is(':checked') ) ? true : false;

and then just add it to your data

data:{value: resourceValues , user: user, checked : checked },
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Actually, you don't even need the ternary operator if you just want to send true / false:

var checked = $(this).is(':checked');
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
Actually this.checked will do just fine.

You don't need to wrap in a jQuery object and then use .is - the checked property is set on the HTML Node.

Author

Commented:
Thank you all three solutions worked.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial