Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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"
});
});
@Sco0byDope

This comment has been minimized.

Copy link

Sco0byDope commented Apr 21, 2017

gj man, helped me to fix one stupid mistake in writin the same aja-script

@dxacaniest

This comment has been minimized.

Copy link

dxacaniest commented Dec 11, 2017

its seem not working with jquery validation js...
can u make example for jquery validation js?

@prashanp16

This comment has been minimized.

Copy link

prashanp16 commented Jan 2, 2018

I am getting the following error that says 1. "console is not defined." 2. "swal is not defined" 3. "data is defined but never used."
Can you help me to solve this?

@tunjicodeit

This comment has been minimized.

Copy link

tunjicodeit commented Jun 24, 2018

prashanp16 - do the following

-Check your input field name
-Check make sure you import all the sweet alert library
Thats all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.