Hello and welcome to our community! Is this your first visit?
Register
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
    Posts
    49
    Thanks
    13
    Thanked 0 Times in 0 Posts

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

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

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    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:
    <?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
    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
    •