I'm new to AJAX and JavaScript, but decided to tackle sending a value based on an HTML "" tag using Ajax to a Python handler. Below is my code, simulated from this site. The problem I'm having is understanding when a box is "checked" vs "unchecked", how does the value get passed, when I have "unchecked" in the input tag? I'm not getting the connection as I want to make sure that whatever the user clicks gets passed through AJAX to my Python Handler.
send_data = function(status) {
$.ajax({
url: "/subscription",
dataType: "json",
data: {'status' : status},
type: "POST",
cache: false
}).done(function(data, status, xml) {
var obj = jQuery.parseJSON(data);
alert(obj.success);
}).fail(function(jqXHR, textStatus, errorThrown) {
}).always(function() {
});
}
$(document).ready(function() {
$("#subscription").submit(function() {
var cb = $("input#switch-1");
if (cb.is("checked")) {
send_data(cb.val());
} else {
send_data(cb.val());
}
return false;
});
});
<form id="subscription" action="">
<label class="mdl-switch mdl-js-switch mdl-js-ripple-effect" for="switch-1">
<input type="checkbox" id="switch-1" name="status" class="mdl-switch__input" id="status" unchecked />
<span class="mdl-switch__label">USEREMAIL Subscribed</span>
<input type="submit" value="Submit" />
</form>
</div>