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
    Dec 2010
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Form submission not sending correct response?

    High I am using javascript file to validate my form fields and return a response, but I keep receiving my error message instead of my success message. I was wondering if there is an error in my php script? Any help is greatly appreciated.

    PHP Code:
    <?php header('Content-type: application/json; charset=utf-8');
    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 'bookings@roustaboutblues.com';
                
    $body="
    <br><hr><br>
    Name: $name <br>
    Phone: $phone <br>
    Email: $email <br>
    I am: $type <br>
    Need help with financing: $financing <br>
    First home: $first_time <br>
    Message: $message
    "
    ;      
                      
                
                
    $headers .= "Content-type: text/html\r\n";
                
    $headers .= "From: $email\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 interest " .$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);                              
              }    
            }
    }

    ?>

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by bos.the.golfer View Post
    High I am using javascript file to validate my form fields and return a response, but I keep receiving my error message instead of my success message. I was wondering if there is an error in my php script? Any help is greatly appreciated.

    PHP Code:
    <?php header('Content-type: application/json; charset=utf-8');
    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 'bookings@roustaboutblues.com';
                
    $body="
    <br><hr><br>
    Name: $name <br>
    Phone: $phone <br>
    Email: $email <br>
    I am: $type <br>
    Need help with financing: $financing <br>
    First home: $first_time <br>
    Message: $message
    "
    ;      
                      
                
                
    $headers .= "Content-type: text/html\r\n";
                
    $headers .= "From: $email\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 interest " .$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);                              
              }    
            }
    }

    ?>
    Which error message are you receiving? "Problem with submission" or "The request was successful but your form is not filled out correctly"?
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.


  •  

    Posting Permissions

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