Page 1 of 2

Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 3:56 pm
by newguy11
HI guys
I really need your help in fixing this PHP script so that it works with the site www.ashfordhales.co.uk the contact form at the bottom. I just don't know what's going wrong?

Code: Select all

<?php  
if( isset($_POST) ){  
  
    //form validation vars  
    $formok = true;  
    $errors = array();  
  
    //submission data  
    $ipaddress = $_SERVER['REMOTE_ADDR'];  
    $date = date('d/m/Y');  
    $time = date('H:m:s');  
  
    //form data  
    $name = $_POST['name'];  
    $email = $_POST['email'];      
    $message = $_POST['message'];  
  
    //validate form data  
  
    //validate name is not empty  
    if(empty($name)){  
        $formok = false;  
        $errors[] = "You have not entered a name";  
    }  
  
    //validate email address is not empty  
    if(empty($email)){  
        $formok = false;  
        $errors[] = "You have not entered an email address";  
		
    //validate email address is valid  
    }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){  
        $formok = false;  
        $errors[] = "You have not entered a valid email address";  
    }  
  
    //validate message is not empty  
    if(empty($message)){  
        $formok = false;  
        $errors[] = "You have not entered a message";  
    }  
    //validate message is greater than 20 characters  
    elseif(strlen($message) < 20){  
        $formok = false;  
        $errors[] = "Your message must be greater than 20 characters";  
    }  
  
    //send email if all is ok  
    if($formok){  
        $headers = "From: ashfordhales@gmail.com" . "\r\n";  
        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
  
        $emailbody = "<p>You have received a new message from the enquiries form on your website.</p> 
                      <p><strong>Name: </strong> {$name} </p> 
                      <p><strong>Email Address: </strong> {$email} </p> 
                      <p><strong>Message: </strong> {$message} </p> 
                      <p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";  
  
        mail("ashfordhales@gmail.com","New Enquiry",$emailbody,$headers);  
  
    }  
  
    //what we need to return back to our form  
    $returndata = array(  
        'posted_form_data' => array(  
            'name' => $name,  
            'email' => $email,   
            'message' => $message  
        ),  
        'form_ok' => $formok,  
        'errors' => $errors  
    );  
  
    //if this is not an ajax request  
    if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){  
        //set session variables  
        session_start();  
        $_SESSION['cf_returndata'] = $returndata;  
  
        //redirect back to form  
        header('location: ' . $_SERVER['HTTP_REFERER']);  
    }  
}  

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 4:08 pm
by Celauran
newguy11 wrote:I just don't know what's going wrong?
You'll have to give us a little more than that. The form seems to submit OK. Are you not getting the mails?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 4:10 pm
by lorenzo-s
Tell us what's the output. A PHP error (or warning)? One of the error messages you coded? Nothing? An ok message but the mail is not sent?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 4:21 pm
by newguy11
No I am not receiving any emails from the form at all?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 4:23 pm
by newguy11
I dont recieve anything from the form it just seems to refresh the page? No warnings or error messages.

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 6:01 pm
by Celauran
There isn't anything actually wrong with your block of code. If you're not receiving mail, I'd check your mail server log. Have you tried a simple PHP page with just a mail() call? As to why error messages aren't being displayed, I couldn't say. $_SESSION['cf_returndata'] is being written fine, but I don't see the code where you make use of it.

EDIT: Although I'd change if (isset($_POST)) to if (!empty($_POST)) as $_POST is always set.

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 6:57 pm
by newguy11
ok so now i have changed the code to this:

Code: Select all

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: ashfordhales'; 
    $to = 'ashfordhales@gmail.com'; 
    $subject = 'Inquiry';
			
    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
				
    if ($_POST['submit']) {				 
        if (mail ($to, $subject, $body, $from)) { 
	    echo '<p>Your message has been sent!</p>';
	} else { 
	    echo '<p>Something went wrong, go back and try again!</p>'; 
	} 
    } 
?>
and it keeps giving me the "Something went wrong, go back and try again" message everytime i submit the form.
Any ideas on whats happening?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:05 pm
by Celauran
Looks like the mail() call is failing. Have you checked your mail server logs as I suggested? Have you tried a page with a hardcoded mail() call and nothing else? Does that work?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:08 pm
by newguy11
how do i check the mail server logs?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:19 pm
by Celauran
They're usually in /var/log/

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:31 pm
by newguy11
Im fairly new at this sorry.
Where do i find /var/log/ ?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:36 pm
by Celauran
Are you running this on your own machine or is this on a shared host?

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:42 pm
by newguy11
Its hosted by 123-reg

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:44 pm
by Celauran
What does this produce?

Code: Select all

$success = mail($your_email_address, 'Test', 'Test Message');
var_dump($success);
(obviously change $your_email_address with your actual email address)

Re: Need help with a PHP contact form!

Posted: Thu Oct 13, 2011 7:49 pm
by newguy11
now it says:
Something went wrong, go back and try again!

bool(false)

???