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

    PHP not returning my Success message.

    Hi all, I am extremely new to PHP and am having an issue with my message after they submit the form. I receive the email and all the information, but there screen returns the error message instead of the success message. Any help is greatly appreciated.

    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 'bos.the.golfer@gmail.com';
                
    $body="
    <br><hr><br>
    <strong>Name:</stong> $name <br>
    <br>
    <strong>Phone:</stong> $phone <br>
    <br>
    <strong>Email:</stong> $email <br>
    <br>
    <strong>Details:</stong> $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);                              
              }    
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Doesn't look like a problem from this end. Disable your javascript, does the encoded result reflect that of success or error messages? From the looks of your conditional structure, it is impossible for it to both mail and to trigger an error.

  • Users who have thanked Fou-Lu for this post:

    bos.the.golfer (01-26-2012)

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for your response. How could I fix it so that it would mail it and send a response? This is only the second time ever trying to script php

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    First you have to determine what the issue is. Like I said, it looks fine from what I see, the conditions are correct (minus a message which indicates a possible mail failure itself, but that is really up to you if you want to display that). So my immediate suspicion is that this is a caching issue with your javascript.
    As of right now, it sends an email AND it provides a successful result. Which is why I'm leaning to an issue caused by your javascript, not the PHP code.

  • Users who have thanked Fou-Lu for this post:

    bos.the.golfer (01-26-2012)

  • #5
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you for the response I will take a look at the javascript as it has to be one of the last lines there and that is the message that pops up. But this is my first try at javascript form validation. By chance do you know javascript as well as you do php?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    No, my javascript is from when the DOM was first adopted by browsers. So like, 8 years ago :/
    I'd just disable JS in my browser and see what PHP does. If it still says failed and you still get an email, something is definitely wrong with PHP.

  • Users who have thanked Fou-Lu for this post:

    bos.the.golfer (01-26-2012)

  • #7
    Regular Coder
    Join Date
    Jan 2012
    Posts
    271
    Thanks
    2
    Thanked 65 Times in 65 Posts
    what is the exact error message showing on screen?

  • Users who have thanked jmj001 for this post:

    bos.the.golfer (01-27-2012)

  • #8
    New Coder
    Join Date
    Dec 2010
    Posts
    12
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Its the error message from my js script. I will paste it below:

    Code:
    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();
    		}
    	});	
    };


  •  

    Posting Permissions

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