Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jan 2012
    Thanked 0 Times in 0 Posts

    Simple php mail script needed to fit in with juery file.


    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>
    			<!-- 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">   	
                            <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> 
    			<!-- END CONTACT FORM -->

    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;
    Last edited by stephen_; 07-20-2012 at 11:52 PM.

  • #2
    Super Moderator
    Join Date
    May 2002
    Perth Australia
    Thanked 101 Times in 99 Posts
    in send.php name,email,subject and message are all available via $_GET a simplest example of send.php would be
    PHP Code:
    $subject $_GET['subject'];
    $message $_GET['message'];
    $headers 'From: webmaster@example.com' "\r\n" .
    'Reply-To: webmaster@example.com' "\r\n" .
    'X-Mailer: PHP/' phpversion();

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

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • Users who have thanked firepages for this post:

    stephen_ (07-24-2012)


    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts