-
-
Save bloodyowl/4182938 to your computer and use it in GitHub Desktop.
AJAX Contact Form with PHP
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>Welcome to my Website</title> | |
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> | |
<script type="text/javascript" src="send.js"></script> | |
</head> | |
<body> | |
<h1>A Simple AJAX Contact Form</h1> | |
<form method="post" action="" id="contact_form"> | |
<input type="text" name="name" placeholder="Name"> | |
<input type="email" name="email" placeholder="Email"> | |
<textarea name="message" placeholder="Message"></textarea> | |
<input type="submit" value="Send"> | |
</form> | |
<span id="notice" style="display:none;">Thanks. We will get back to you soon.</span> | |
</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 | |
//Get the Form Values | |
if(isset($_POST['name']){ | |
//Make Variables Safe | |
$name = mysql_real_escape_string(trim($_POST['name']); | |
$email = mysql_real_escape_string(trim($_POST['email']); | |
$message = mysql_real_escape_string(trim($_POST['message']); | |
mysql_connect('localhost', 'root', 'password'); | |
$query = ('INSERT INTO contacts ('name', 'email', 'message') VALUES($name, $email, $message); | |
$result = mysql_query($query) or die(mysql_error()); | |
} | |
else{ | |
header('Location: index.htm'); | |
} | |
?> |
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
;(function(){ | |
var formElement = $('#contact_form') | |
formElement.submit(function() { | |
var dataString = formElement.serialize() | |
$.ajax({ | |
type : "POST", | |
url : "save.php", | |
data : dataString, | |
cache : false, | |
success : function() { | |
formElement.fadeOut(300) | |
$("#notice").fadeIn(400) | |
} | |
}) | |
return false | |
}) | |
})() |
<?php
//Get the Form Values
if (isset($_POST['name']) {
//Make Variables Safe
mysql_connect('localhost', 'root', 'password');
$name = mysql_real_escape_string(trim($_POST['name']);
$email = mysql_real_escape_string(trim($_POST['email']);
$message = mysql_real_escape_string(trim($_POST['message']);
$query = "INSERT INTO contacts ('name', 'email', 'message') VALUES('$name', '$email', '$message')";
$result = mysql_query($query) or die(mysql_error());
} else {
header('Location: index.htm');
}
Second edit. Fuck PHP btw.
Oh, that's true, I didn't even look at the PHP, it was just a fork to add the FormElement#serialize
JS function & to show the uselessness of setting an id
for each <input>
, <textarea>
<select>
then.
@Rydgel Sorry...Made a Silly Mistake....thanks for correction...:)
@mlbli Looking forward to see the JS function you have...:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The PHP code will fail, here is the correct one: