Last active
July 25, 2023 02:41
-
-
Save sohelrana820/242743aadcb160c5c0a8 to your computer and use it in GitHub Desktop.
Send Email Through AJax
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$data = $_POST['data']; | |
$name = $data['name']; | |
$from = $data['email']; | |
$subject = $data['subject']; | |
$message = $data['message']; | |
$to = 'hayley@studioeighty7.co.uk'; | |
$headers = "MIME-Version: 1.0"; | |
$headers .= "Content-type: text/plain; charset=iso-8859-1"; | |
$headers .= "From: {$name} <{$from}>"; | |
$headers .= "Reply-To: <{$from}>"; | |
$headers .= "Subject: {$subject}"; | |
$headers .= "X-Mailer: PHP/".phpversion(); | |
if(mail($to, $subject, $message, $headers)){ | |
echo 1; | |
} | |
else{ | |
echo 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="main-contact-form" name="contact-form" method="post" action="email.php"> | |
<div class="form-group"> | |
<input type="text" name="name" class="form-control" id="name" placeholder="Name"> | |
</div> | |
<div class="form-group"> | |
<input type="email" name="email" class="form-control" id="email" placeholder="Email"> | |
</div> | |
<div class="form-group"> | |
<input type="text" name="subject" class="form-control" id="subject" placeholder="Subject"> | |
</div> | |
<div class="form-group"> | |
<textarea name="message" class="form-control" rows="8" id="message" placeholder="Message"></textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Send Message</button> | |
</form> | |
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script> | |
<script> | |
var form = $('#main-contact-form'); | |
form.submit(function(event){ | |
event.preventDefault(); | |
var form_status = $('<div class="form_status"></div>'); | |
var data = { | |
name: $('#name').val(), | |
email: $('#email').val(), | |
subject: $('#subject').val(), | |
message: $('#message').val() | |
}; | |
$.ajax({ | |
url: $(this).attr('action'), | |
type: "POST", | |
dataType: "json", | |
data: {'data': data}, | |
beforeSend: function(){ | |
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() ); | |
}, | |
success: function (data) { | |
if(data == 1){ | |
form_status.html('<p class="text-success">Thank you, your message has been sent successfully. We aim to reply as soon as possible.</p>').delay(3000).fadeOut(); | |
} | |
else{ | |
form_status.html('<p class="text-error">Sorry, email not sent</p>').delay(3000).fadeOut(); | |
} | |
}, | |
error: function (xhr, status, error) { | |
console.log(error); | |
} | |
}); | |
}); | |
</script> |
Thanks a lot,
But, i'm receiving only the Message. Not receiving the Name, email or subject. Can you help me on this pls.
Guys, Can anyone please help me in this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Working Awesome!
Thank you.