I wanted to have the contact form in my webpage but I wanted it so that it does not refresh or open a new tab while submission. I followed this question Submit a php form and display its result on div without reloading page and implimented it.
It's working now. It sends an email but does not take values from the contact form. ie: The email body should be
Name: (name entered in the form)
Email: (email entered in the form)
Message: (message entered in the form)
But it's always :
Name:
Email:
Message:
Any help would be greatly appreciated.
Here's my HTML
<!DOCTYPE html>
<html>
<head>
<title>FeedBack Form With Email Functionality</title>
<style>
.result{
padding: 20px;
font-size: 16px;
background-color: #ccc;
color: #fff;
}
#contactme{
position:absolute;
top:100px;
width:833px;
height:450px;
background:#FFF;
font-size:16px;
color:#FFF;
}
#sub_button:hover{
cursor:pointer;
}
#sub_button {
background:#F00;
border: 0;
display: block;
width: 161px;
height: 28px;
}
</style>
</head>
<body>
<div id="contactme" style="font-family: 'Josefin Sans', sans-serif">
<div>
<input type="text" id="name" name="name" value="" placeholder="Name" required="required" autofocus="autofocus" autocomplete="off" />
</div>
<div>
<input type="email" id="email" name="email" value="" placeholder="[email protected]" required="required" autocomplete="off" />
</div>
<div>
<textarea id="message" name="message" value="" placeholder="20 characters max." required="required" maxlength="50" autocomplete="off" ></textarea>
</div>
<div id="sub_button" style="position:absolute; top:30px; right:30px;"></div>
</div>
<div class="result"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#sub_button').click(function() {
$.ajax({
type:"post",
url:"insert1.php",
data: $("#contactme").serialize(),
success: function(response){
$(".result").html(response);
}
});
});
});
</script>
</body>
<!-- body ends here -->
</html>
Here's my PHP :
<?php
// Get values from form
$name=$_POST['name'];
$email=$_POST['email'];
$data=$_POST['message'];
$to = "[email protected]";
$subject = "From The Website";
$message = " Name: " . $name . "\r\n Email: " . $email . "\r\n Message: " . $data;
$from = $name;
$headers = "From:" . $from . "\r\n";
$headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n";
if(@mail($to,$subject,$message,$headers))
{
echo "Message was sent successfully!";
echo '<script>alert("Message was sent successfully!");</script>';
}else{
echo '<script>alert("Error in sending message! Please mail your message to [email protected]");</script>';
echo "Error in sending message! Please mail your message to [email protected]";
}
echo '<script>
self.close();
</script>';
?>