views:

376

answers:

1

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.

+1  A: 

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
  };
 });
});
Mathias Bynens
Can you elaborate please?
Tom
Check my edit :)
Mathias Bynens
Thank you so much! It seems like it would work in theory, but I've no way to really test it. I've hooked it into my email program and submitted a test, but it doesn't seem to email. I'm guessing there is something wrong with my PHP email script. Suggestions?
Tom
That would be a separate issue with your email script. Your best bet is to close this question by marking an accepted answer, and posting a new Stack Overflow question with more details of your email script, tagged with "php form mail email" etc. Be sure to add the source of `email.php` as well!
Mathias Bynens