Solved

contact form doesn't seem to work

Posted on 2011-02-18
12
439 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:wood1e
  • 5
  • 3
  • 2
  • +1
12 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34924600
Can you post the contact.php file ?
0
 
LVL 19

Accepted Solution

by:
Bardobrave earned 125 total points
ID: 34925077
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
 

Author Comment

by:wood1e
ID: 34925088
<?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
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34925155
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
 

Author Comment

by:wood1e
ID: 34925208
There is no thank you page, and it all works fine on another website I have worked on. :)
0
 
LVL 27

Assisted Solution

by:Lukasz Chmielewski
Lukasz Chmielewski earned 125 total points
ID: 34925220
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 34925333
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
 

Author Comment

by:wood1e
ID: 34925699
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 34926045
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 34926076
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
 

Author Comment

by:wood1e
ID: 34926932
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
 

Author Closing Comment

by:wood1e
ID: 34926937
Vary helpful just not what I was looking for.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now