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 ?