Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Forked from kunalvarma05/index.htm
Created December 1, 2012 15:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bloodyowl/4182938 to your computer and use it in GitHub Desktop.
Save bloodyowl/4182938 to your computer and use it in GitHub Desktop.
AJAX Contact Form with PHP
<!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>
<?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');
}
?>
;(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
})
})()
@Rydgel
Copy link

Rydgel commented Dec 1, 2012

The PHP code will fail, here is the correct one:

<?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');
}

@Rydgel
Copy link

Rydgel commented Dec 1, 2012

<?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.

@bloodyowl
Copy link
Author

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.

@kunalvarma05
Copy link

@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