Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save arcanoix/454ccbc4d8f5605d4fad0aef4f4d5c55 to your computer and use it in GitHub Desktop.
Save arcanoix/454ccbc4d8f5605d4fad0aef4f4d5c55 to your computer and use it in GitHub Desktop.
Send Form (HTML+PHP+Sweet Alert)
<?php
$to = 'your@email.com' . "\r\n";
$subject = 'Subject';
$name = $_POST['nombre'];
$email = $_POST['email'];
$message = "************************************************** \r\n" .
"Message from you website! \r\n" .
"************************************************** \r\n" .
"Name: " . $name . "\r\n" .
"E-mail: " . $email . "\r\n" .
"Message: " . $_POST["message"] . "\r\n";
$headers = "From: " . $name . "<" . $email . "> \r\n" .
"Reply-To: " . $email . "\r\n" .
"MIME-Version: 1.0" . "\r\n" .
"Content-type:text/html;charset=UTF-8" . "\r\n";
mail($to, $subject, $message, $headers);
?>
<form role="form" id="contact-form" method="post">
<label for="name">Name</label>
<input type="text" placeholder="Full Name" name="name" required>
<label for="email">Email</label>
<input type="email" placeholder="Your Email" name="email" required>
<label>Message</label>
<textarea name="message" placeholder="Your Message" rows="9"></textarea>
<button type="submit" class="btn">Send</button>
</form>
$(document).ready(function(){
$('#contact-form').on('submit',function(e) { //Don't foget to change the id form
$.ajax({
url:'contact.php', //===PHP file name====
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
//Success Message == 'Title', 'Message body', Last one leave as it is
swal("¡Success!", "Message sent!", "success");
},
error:function(data){
//Error Message == 'Title', 'Message body', Last one leave as it is
swal("Oops...", "Something went wrong :(", "error");
}
});
e.preventDefault(); //This is to Avoid Page Refresh and Fire the Event "Click"
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment