View Full Version : Simple php mail script needed to fit in with juery file.

07-20-2012, 11:27 PM

I've been given a html contact form which has a js file to process and send it yet I need to create a php form to send the details and I'm stuck in how to construct it...

The general idea I think is to (in php)

Please enter your name.
Please enter your email.
Please enter your comment.
Invalid email address.
Back to the html page...


<section id="contact" class="sixteen columns">
<h2>Contact us <a href="#home" class="gotop">top</a></h2>

<div class="form-success eight columns">
<p>Thank you, your message has been sent.</p>

<div id="contact_form" class="eight columns" style="margin:0;">
<h6>Drop us a line, we will be brief in response!</h6>
<div class="contact-form eight columns" style="margin:0;">
<form action="contact-form/send.php" method="post" class="form">
<input class="text" type="text" name="name">

<input class="text" type="text" name="email">

<input class="text" type="text" name="subject">

<textarea name="message" rows="8" cols="60"></textarea>

<a href="javascript:;" id="submit" class="button">Send Email</a>

<div class="loading"></div>

The javascript for processing the form is;


//if submit button is clicked
$('#submit').click(function () {

//Get the data from all the fields
var name = $('input[name=name]');
var email = $('input[name=email]');
var subject = $('input[name=subject]');
var comment = $('textarea[name=message]');

//Simple validation to make sure user entered something
//If error found, add hightlight class to the text field
if (name.val()=='') {
return false;
} else name.removeClass('hightlight');

if (email.val()=='') {
return false;
} else email.removeClass('hightlight');

//E-mail address validation

var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(email.val()) == false) {
return false;
} else email.removeClass('hightlight');

if (comment.val()=='') {
return false;
} else comment.removeClass('hightlight');

//organize the data properly
var data = 'name=' + name.val() + '&email=' + email.val() + '&subject=' +
subject.val() + '&comment=' + encodeURIComponent(comment.val());

//disabled all the text fields
$('.contact input, .contact textarea').attr('disabled','true');

//show the loading sign

//start the ajax
//this is the php file that processes the data and send mail
url: "contact-form/send.php",

//GET method is used
type: "GET",

//pass the data
data: data,

//Do not cache the page
cache: false,

success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form

//show the success message

//if process.php returned 0/false (send mail failed)
} else alert('An unexpected error occured. Please try again later.');

//cancel the submit button default behaviours
return false;

07-21-2012, 01:47 AM
in send.php name,email,subject and message are all available via $_GET a simplest example of send.php would be

$to = $_GET['email'];
$subject = $_GET['subject'];
$message = $_GET['message'];
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

echo mail($to, $subject, $message, $headers);

note though that though you have done some validation in your javascript, I would not trust that alone and you should validate in send.php as well