Hi, I was wondering if it was possible to write some jQuery to check if on a button click, check if a certain checkbox is checked, and carry out an AJAX request to a PHP file.
The response if successful should post the JSON values to the "results" and "results2" div on my main html page.
I have tried the following code below
<form action="PHP.php" id="test" method="post">
<input type="checkbox" name="check1" value="1"/>one<br />
<input type="checkbox" name="check2" value="2"/>two<br />
<input type="checkbox" name="check3" value="3"/>three<br />
<input type="submit" id="submit_button" name="sub" value="Show"/>
</form>
<script>
$('#submit_button').click(function(){
event.preventDefault();
if ($('.check1').is(':checked')) {
$.ajax({
url: 'PHP.php',
type: 'POST',
data: json,
success: function(data){
$("#results").empty();
$("#results").append(data);
console.log(data);
}
});
}
if ($('.check2').is(':checked')) {
$.ajax({
url: 'PHP2.php',
type: 'POST',
data: json,
success: function(data){
$("#results2").empty();
$("#results2").append(data);
console.log(data);
}
});
}
});
</script>
</div>
<div id="results">
</div>
<div id="results2">
</div>
Note: the PHP.php and PHP2.php file which I am loading I am only echoing JSON at the moment, until I can get this working, but the console is not logging any data at all.
I am having a bit of trouble in getting this to work. I was hoping for some help, or some guidance on the best way to do this?
Thanks in advance.
data: jsontodataType: 'json'checkboxesit should be same and applyclassto each checkbox else you can't get it$('.check1').is(':checked')class, Thank you!