Last active
August 29, 2015 14:24
-
-
Save himstar/e73775ea998034a0b2e5 to your computer and use it in GitHub Desktop.
Ajax email contact form without refreshing page
This file contains hidden or 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
| <html> | |
| <head> | |
| <title>Simple Ajax Contact Form - TricksWay</title> | |
| <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> | |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> | |
| <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> | |
| <script type="text/javascript"> | |
| $(document).ready(function(){ | |
| $( "#submitBtn" ).click(function( event ) { | |
| //values | |
| var name=document.getElementById('name').value; | |
| var email=document.getElementById('email').value; | |
| var message=document.getElementById('message').value; | |
| var dataString = {"name": name, "email":email, "message":message} | |
| $.ajax({ | |
| type:"post", | |
| url:"mail.php", | |
| data: dataString, | |
| success: function(html) { | |
| $('#feedback').html(html); | |
| } | |
| }); | |
| event.preventDefault(); | |
| }); | |
| }); | |
| </script> | |
| </head> | |
| <body> | |
| <div class="container"> | |
| <center><div id="feedback" class="alert alert-success"></div></center> | |
| <form class="form-group"> | |
| <input type="text" class="form-control" name="name" id="name" value="" placeholder="Your Name..."> | |
| <br> | |
| <input type="email" class="form-control" name="email" id="email" value="" placeholder="Your Email..."> | |
| <br> | |
| <textarea id="message" class="form-control" cols="40" rows="8" name="message" placeholder="This is a textarea"></textarea> | |
| <br> | |
| <button type="submit" id="submitBtn" class="btn btn-info">Send Mail</button> | |
| </form> | |
| </div> | |
| </body> | |
| </html> |
This file contains hidden or 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 | |
| $to = "[email protected]"; // Your email address will be here | |
| if (!isset($_POST[name]) || empty($_POST[name])) | |
| { | |
| echo "You didn't fill in the NAME field!"; | |
| exit; | |
| } | |
| else | |
| { | |
| $name = $_POST[name]; | |
| } | |
| $subject = "Website Contact Form - TricksWay Demo"; | |
| if (empty($_POST["email"])) { | |
| echo "You didn't fill in the FROM field!"; | |
| exit; | |
| } else { | |
| // check if e-mail address is well-formed | |
| if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| echo "You filled Invalid FROM field!"; | |
| exit; | |
| } | |
| else | |
| { | |
| $email = $_POST[email]; | |
| } | |
| } | |
| if (!isset($_POST[message]) || empty($_POST[message])) | |
| { | |
| echo "You didn't fill in the MESSAGE field!"; | |
| exit; | |
| } | |
| else | |
| { | |
| $message = $_POST[message]; | |
| } | |
| $headers = "MIME-Version: 1.0" . "\r\n"; | |
| $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; | |
| $headers .= "From: " . $email . " <" . $email . ">" . "\r\n"; | |
| if (mail($to, $subject, $message, $headers)) | |
| { | |
| echo"<p>Email Sent !!!</p>\n"; | |
| } | |
| else | |
| { | |
| echo"<p>Error!</p>\n"; | |
| echo"<p>The mail() function failed.</p>"; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment