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 1 of 1
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Form Submission....

    I have the below code to validate and send my form. It sends the email fine, but I receive the error message at the bottom of the page instead of the success message. Any help on why it is doing this would be great. This is my first form validation script. Here is the page live: Website

    here is the javascript:

    Code:
    $(document).ready(function() {
    	
    	$('form #response').hide();
    	
    	$('#submitbtn').click(function(e) {
    		
    		e.preventDefault();
    				
    		var valid = '';
    		var required = ' is required.';
    		var name = $('form #name').val();
    		var phone = $('form #phone').val();
    		var email = $('form #email').val();
    		var details = $('form #details').val();
    		var honeypot = $('form #honeypot').val();
    		var humancheck = $('form #humancheck').val();
    		
    		if (!name.match(/^[A-Za-z ]{3,20}$/)) {
    			valid = '<p>- Your name' + required +'</p>';	
    		}
    		
    		if (!phone.match(/^([1]-)?[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/i)) {
    			valid += '<p>- A valid phone number' + required +'</p>';												  
    		}
    		if (!email.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
    			valid += '<p>- A valid email address' + required +'</p>';												  
    		}
    		
    		if (honeypot != 'http://') {
    			valid += '<p>Spambots are not allowed.</p>';	
    		}
    		
    		if (humancheck != '') {
    			valid += '<p>A human user' + required + '</p>';	
    		}
    		
    		if (valid != '') {
    			
    			$('form #response').removeClass().addClass('error')
    				.html('<strong>Please correct the errors below:</strong>' + 
    				valid).fadeIn('fast');			
    		}
    		
    		else {
    			
    			$('form #response').removeClass().addClass('processing').html('Processing...').fadeIn('fast');										
    			
    			var formData = $('form').serialize();
    			submitForm(formData);			
    		}			
    			
    	});
    });
    
    function submitForm(formData) {
    	
    	$.ajax({	
    		type: 'POST',
    		url: 'bookings.php',		
    		data: formData,
    		dataType: 'json',
    		cache: false,
    		timeout: 7000,
    		success: function(data) { 			
    			
    			$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
    						.html(data.msg).fadeIn('fast');	
    						
    			if ($('form #response').hasClass('success')) {
    				
    				setTimeout("$('form #response').fadeOut('fast')", 5000);
    			}
    		
    		},
    		error: function(XMLHttpRequest, textStatus, errorThrown) {
    						
    			$('form #response').removeClass().addClass('error')
    						.html('<strong>There was an error. Please use the above information and contact us directly.</strong>').fadeIn('fast');			
    		},				
    		complete: function(XMLHttpRequest, status) { 			
    			
    			$('form')[0].reset();
    		}
    	});	
    };
    PHP Code:

    PHP Code:
    <?php 

    sleep
    (4);

    $name trim(stripslashes(htmlspecialchars($_POST['name'])));              
    $phone trim(stripslashes(htmlspecialchars($_POST['phone'])));        
    $email trim(stripslashes(htmlspecialchars($_POST['email'])));
    $details trim(stripslashes(htmlspecialchars($_POST['details'])));        
    $humancheck $_POST['humancheck'];
    $honeypot $_POST['honeypot'];


    if (
    $honeypot == 'http://' && empty($humancheck)) {    
            
            
    //Validate data and return success or error message
            
    $error_message '';    
            
    $name_check_exp "/^[A-Za-z ]{3,20}$/";
            
    $phone_check_exp "/^([1]-)?[0-9]{3}-?[0-9]{3}-?[0-9]{4}$/i";
            
    $email_check_exp "/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,4}$/";
            
            if (!
    preg_match($name_check_exp$name)) {
                        
                        
    $error_message .= "<p>Your name is required.</p>";               
            }
            if (!
    preg_match($phone_check_exp$phone)) {
                        
                        
    $error_message .= "<p>A valid phone number is required.</p>";    
            }        
            if (!
    preg_match($email_check_exp$email)) {
                        
                        
    $error_message .= "<p>A valid email address is required.</p>";    
            }
            if (!empty(
    $error_message)) {
                        
    $return['error'] = true;
                        
    $return['msg'] = "<h3>Oops! The request was successful but your form is not filled out correctly.</h3>".$error_message;                    
                        echo 
    json_encode($return);
                        exit();
            } else {
                
                
    //send to  an email
                
                
    $emailSubject 'Bookings Form';
                
    $webMaster 'some.email@gmail.com';
                
    $body="
    <br><hr><br>
    Name: $name <br>
    Phone: $phone <br>
    Email: $email <br>
    Details: $details 
    "
    ;      
                
                
    $headers "From: $email\r\n";
                
    $headers .= "Content-type: text/html\r\n";
        

                
    //send email and return to user
                
    if(mail($webMaster$emailSubject$body$headers)) {
                
                    
    $return['error'] = false;
                    
    $return['msg'] = "<p>Message sent successfully. Thank you for your intrest " .$name .".</p>"
                    echo 
    json_encode($return);
        
                } else {
        
                        
    $return['error'] = true;
                        
    $return['msg'] = "<h3>Oops! There was a problem with your submission. Please try again.</h3>";    
                        echo 
    json_encode($return);
                                  
              }        
        }
    }

     
    ?>
    Last edited by bos.the.golfer; 01-25-2012 at 11:14 PM. Reason: adding PHP script


 

Posting Permissions

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