contact form doesn't seem to work

Hi, just installed a contact form on my website, which works...ie emails are sent.

but the Thank You message does not appear the detials I input to test that the actual email works seem to have stuck in the form.  which I know is just showing on my browser and needs cache clearing.  so that is not really an issue

It is the Thank You message not showing and the 'This field is required' text showing which I am sure should only be seen if someone doesn't enter something.

Any advice grately recevied. URL below
url.txt
wood1eAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lukasz ChmielewskiCommented:
Can you post the contact.php file ?
0
BardobraveCommented:
So it seems you have problems with the returning of your form (as the sending is working, because your email is sent).

What are you doing on your code after sending mail?

You should redirect the execution back to your view and alter it to show the result of the sending (validating data if there's not inserted in the previous form or showing the Thank you message if all it's ok).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wood1eAuthor Commented:
<?php

// get posted data into local variables
$EmailFrom = "noreply@mywebadress.co.uk";
$EmailTo = "info@mywebaddress.co.uk";
$Subject = "Website Enquiry";
$Name = Trim(stripslashes($_POST['Name'])); 
$Email = Trim(stripslashes($_POST['Email'])); 
$Telephone = Trim(stripslashes($_POST['Telephone'])); 
$Message = Trim(stripslashes($_POST['Message'])); 

 
if (empty($Name))
{
$errors[] = 'Please enter a name';
}

if (empty($Email))
{
$errors[] = 'Please enter an e-mail';
}
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $Email))
{
$errors[] = 'Please enter a valid e-mail address';
}

if (empty($Telephone))
{
$errors[] = 'Please enter a Telephone number';
}
else if (!is_numeric($Telephone))
{
$errors[] = 'Please enter a valid Telephone number';
}


if (count($errors) == 0)
{
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "\n";
$Body .= "Telephone: ";
$Body .= $Telephone;
$Body .= "\n";
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
$Body .= "\n";

// send email 
mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>", $headers);


// redirect to success page 
if ($success){
  print "Thank you for your enquiry";
}
}
else
{
echo $errors[0];
}

?>

Open in new window

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

BardobraveCommented:
Mmmm.... I'm not used to PHP, but I supose that

// redirect to success page
if ($success){
  print "Thank you for your enquiry";
}

is not a redirection, so after you send your form to this php file, your execution doesn't return to view, and thus, your page is freezed.

Maybe you have to create an html page with the message and redirect to it from this point of code.
0
wood1eAuthor Commented:
There is no thank you page, and it all works fine on another website I have worked on. :)
0
Lukasz ChmielewskiCommented:
if ($success){
  print "Thank you for your enquiry";
}

This checks if boolean value of $success variable is true. It is not, because there isn't one, you would have to set it at the top of the script to $success = true;
Any error would exit the script then and not echo the success at the bottom.
The other thing is what Bardobrave wrote - you do not handle redirection or ajax for the form. Is it what you want ?
0
Ray PaseurCommented:
Contact forms are a pretty well understood part of the technology today.  If you are a beginner in PHP, this book will be helpful:
http://www.sitepoint.com/books/phpmysql4/

When you are developing, you want to add error_reporting(E_ALL) to your script.  If you had that, you would have received a notice that $success was undefined.  Unfortunately PHP allows you to use undefined variables as if they were NULL, and in the default installation of PHP, the notices are suppressed.  So you never know that you are relying on something that does not exist.

Instead of testing with if($success) you might want to test with if (!empty($errors)) - since the code fills in the $errors array if there are errors.
0
wood1eAuthor Commented:
Sorry Roads_Roads you have completely lost me!! :)

Ray_Paseur: I understand a little better what you are saying...but just to clarify...what are you saying I should put in the code? or more importantly...how :)
0
Ray PaseurCommented:
Here is a teaching example showing the moving parts of a form-to-email script.


<?php // RAY_form_to_email.php
error_reporting(E_ALL);

// SEND MAIL FROM A FORM

// A FUNCTION TO CLEAN UP THE DATA - AVOID BECOMING AN OPEN-RELAY FOR SPAM
function clean_string($str)
{
    $str = stripslashes($str);
    $str = trim(preg_replace("/ +/", " ", $str));
    $str = preg_replace('/^ a-zA-Z0-9&+:?_\.\-/', '', $str);
    return $str;
}

// REQUIRED VALUES ARE PREPOPULATED - CHANGE THESE FOR YOUR WORK
$from  = "NoReply@Your.org";
$subj  = "Contact Form";

// THIS IS AN ARRAY OF RECIPIENTS
$to[]  = "You@Your.org";
$to[]  = "Her@Your.org";
$to[]  = "Him@Your.org";

// IF THE DATA HAS BEEN POSTED
if (!empty($_POST['email']))
{
    // CLEAN UP THE POTENTIALLY BAD AND DANGEROUS DATA
    $email      = clean_string($_POST["email"]);
    $name       = clean_string($_POST["name"]);
    $telephone  = clean_string($_POST["telephone"]);

    // CONSTRUCT THE MESSAGE
    $content    = '';
    $content   .= "You have a New Query From $name" . PHP_EOL . PHP_EOL;
    $content   .= "Tel No: $telephone" . PHP_EOL;
    $content   .= "Email: $email" . PHP_EOL;

    // SEND MAIL TO EACH RECIPIENT
    foreach ($to as $recipient)
    {
        if (!mail( $recipient, $subj, $content, "From: $from\r\n"))
        {
            echo "MAIL FAILED FOR $recipient";
        }
        else
        {
            echo "MAIL WORKED FOR $recipient";
        }
    }
} // END OF PHP - PUT UP THE FORM
?>
<form method="post">
<br/>Email: <input name="email" />
<br/>Phone: <input name="telephone" />
<br/>Name:  <input name="name" />
<br/><input type="submit" />
</form>

Open in new window

0
Ray PaseurCommented:
Here is how to validate an email address.  The POSIX regular expressions using ereg() functions are deprecated, so you will want to get rid of that ASAP.
http://us.php.net/manual/en/ref.regex.php

If you read these example scripts over carefully and you are still not sure how to change your code, you might want to consider hiring a professional developer to help you.  EE is a good place to get answers to your questions, but maybe not the best place to get someone to write code for free.
<?php // RAY_email_validation.php
error_reporting(E_ALL);


// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
function check_valid_email($email)
{
    // IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
    // MAN PAGE: http://us3.php.net/manual/en/intro.filter.php
    if (strnatcmp(phpversion(),'5.2') >= 0)
    {
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) return FALSE;
    }
    // IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
    else
    {
        $regex
        = '/'                       // START REGEX DELIMITER
        . '^'                       // START STRING
        . '[A-Z0-9_-]'              // AN EMAIL - SOME CHARACTER(S)
        . '[A-Z0-9._-]*'            // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
        . '@'                       // A SINGLE AT-SIGN
        . '([A-Z0-9][A-Z0-9-]*\.)+' // A DOMAIN NAME PERMITS DOT, ENDS DOT
        . '[A-Z\.]'                 // A TOP-LEVEL DOMAIN PERMITS DOT
        . '{2,6}'                   // TLD LENGTH >= 2 AND =< 6
        . '$'                       // ENDOF STRING
        . '/'                       // ENDOF REGEX DELIMITER
        . 'i'                       // CASE INSENSITIVE
        ;
        if (!preg_match($regex, $email)) return FALSE;
    }

    // FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
    $domain = explode('@', $email);

    // MAN PAGE: http://us3.php.net/manual/en/function.checkdnsrr.php
    if ( checkdnsrr($domain[1],"MX") || checkdnsrr($domain[1],"A") ) return TRUE;

    // EMAIL IS NOT ROUTABLE
    return FALSE;
}


// DEMONSTRATE THE FUNCTION IN ACTION
$e = '';
if (!empty($_GET["e"]))
{
    $e = $_GET["e"];
    if (check_valid_email($e))
    {
        echo "<br/>VALID: $e \n";
    } else
    {
        echo "<br/>BOGUS: $e \n";
    }
}

// END OF PROCESSING - PUT UP THE FORM
$form = <<<ENDFORM
<form method="get">
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;

echo $form;

Open in new window

0
wood1eAuthor Commented:
Thanks guys, but I was just looking wanted an answer...Sorry I don't have time to learn... :) Not at the moment anyway.

I will split the points... :)
0
wood1eAuthor Commented:
Vary helpful just not what I was looking for.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.