...

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



stephen_
07-20-2012, 10:27 PM
Hi,

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

HTML:


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

<!-- START CONTACT FORM -->
<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">
<span>Name</span>
<input class="text" type="text" name="name">

<span>E-Mail</span>
<input class="text" type="text" name="email">

<span>Subject</span>
<input class="text" type="text" name="subject">

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

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

<div class="loading"></div>
</form>
</div>
</div>
<!-- END CONTACT FORM -->



The javascript for processing the form is;


jQuery(document).ready(function(){

//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()=='') {
name.addClass('hightlight');
return false;
} else name.removeClass('hightlight');

if (email.val()=='') {
email.addClass('hightlight');
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) {
email.addClass('hightlight');
return false;
} else email.removeClass('hightlight');

if (comment.val()=='') {
comment.addClass('hightlight');
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
$('.loading').show();

//start the ajax
$.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
success: function (html) {
//if process.php returned 1/true (send mail success)
if (html==1) {
//hide the form
$('.contact-form').hide(600);

//show the success message
$('.form-success').fadeIn('slow');

//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;
});
});

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


<?php
$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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum