Jquery OnSubmit If Checkbox is Checked Send an Email
-
19-09-2019 - |
Question
I have an existing form that I would like to add email functionality to. There is a field in my form called "their email", and just under it a checkbox that asks "Send Them an email?". I would like a jquery script that would take the email from the "their email" field and email them (via an external email.php file) if the checkbox is checked. If not, completely ignore it. This would all have to take place upon submission of the form.
I currently have this:
if($('#checkbox').is(':checked')){
alert('It is checked');
}else{
alert('Not checked');
}
Just to test it out, but I cannot get it to alert either way once I submit it.
Solution
You’re looking for $.post()
.
$(function() {
$('form').submit(function() {
if($('#checkbox').is(':checked')) {
// It is checked
$.post('email.php', { email: $('input[name=email]').val() });
} else {
// It’s not checked
};
});
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow