Hi!
I need to know when the php file has sent the email from the a HTML Form and passsing it to Jquery. i did some research in internet and i found the "submit" option with .serialize() in Jquery, but 'cause i'm new in php i can't get it work.
How .serialize() works exactly?
Because i'm having my "alert" just by clicing the submit bottom and not when the email has been sent.
If someone could help me with a link to begginers to learn that or make a correction in my code, i would be very greatfull.
Here's my code :
HTML :
<html>
<form method="post" action="form-to-email.php" id="myform">
<div class="field">
<label for="name">Your Name :</label>
<input type="text" name="name" id="name" class="required" />
</div>
<div class="field">
<label for="email">Please, give us an email :</label>
<input type="text" name="email" id="email" class="required email" />
</div>
<div class="field">
<label for="message" id="message-bg">Tell us about your project :</label>
<textarea name="message" rows="20" cols="20" id="message" class="required"></textarea>
</div>
<img id="icon-contact" src="images/icon-contact.png" alt="icon-contact" />
<div class="field">
<input type="submit" name="submit" value="Submit" class="submit-button" />
</div>
</form>
</html>
PHP :
<?php
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
//Validate first
if(empty($name)||empty($visitor_email))
{
echo "Name and email are mandatory!";
exit;
}
if(IsInjected($visitor_email))
{
echo "Bad email value!";
exit;
}
$email_from = '
[email protected]';//<== update the email address
$email_subject = "Mensaje del amigo/a $name";
$email_body = "Hola! Me llamo $name, y quisiera decirles :\n \n$message \n \n \n".
$to = "
[email protected]";//<== update the email address
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
header('Location: index.html');
?>
JQUERY :
$("#myform").submit(function(event) {
/* stop form from submitting normally */
event.preventDefault();
$.post( 'form-to-email.php', $("#myform").serialize(),
function() {
$('#myform').get(0).reset();
alert("Your message has been sent, Thanks!") ;
}
);
});
Thanks a lot for your help !