PHP form to email - help the edit the script

Hi,
I am a beginner in php. I speak little english I got the the script on form to email but I have problems with him. Please help with that?

  1. Form are uploaded but immediately sends unfilled on email.
  2. Immediately after loaded and sent to the e-mail page appears thanking although has not been filled. Is perhaps a better to control of
    fulfillment of important data ?
  3. How to do after sending the form redirection to the new webpage with the results:
    a) Form was sent - web page I have already made - the need to insert link to a php.
    b) Error + error list - web page I have already made - the need to insert link to a php.

Thank you all in advance for your help with the addition of a script !!!
I enclose a script:

[php]

<?php $EmailFrom = "info@my mail.co.uk"; $EmailTo = "info@my mail.co.uk"; $Subject = "Info"; $Name = Trim(stripslashes($_POST['Name'])); $Email = Trim(stripslashes($_POST['Email'])); $Message = Trim(stripslashes($_POST['Message'])); $validationOK=true; if (!$validationOK) { echo "please check your details"; header("Location: http://my webpage/testform.php"); exit; } $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Message; $Body .= "\n"; $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success){ print ""; } else{ print ""; } ?>
<form action="testform.asp" method="post">
Name:
Email Address:
Comments:
To prevent spam, please enter the text from the following picture:

[/php]

When you test if the mail was successfully sent, why do you redirect to the same page regardless? If you want to show errors, you need to show errors, not redirect to a thank you page.

Well, willH, your code does exactly what you asked of it. It is just a logic issue.
Your code:
$validationOK=true;
if (!$validationOK) {
echo “please check your details”;
header(“Location: http://my webpage/testform.php”);
exit;
}
Sets the validationOK to true and then does not do the rest of the validation. Normally, you would set the
validation variable to true and then validate the inputs. If one of the inputs are not valid, you would reset
the validationOK variable to false. Therefore, it would be something like:
$validationOK=true;
// Check inputs here to see if they are valid. Many ways to do that. Such as checking for empty inputs.
if (!$validationOK) {
echo “please check your details”;
header(“Location: http://my webpage/testform.php”);
exit;
}
Also, after that the code continues. Therefore, your code will send out the email no matter what the result
of the validations are. Normally you would do it more like this:
$validationOK=true;
// Here check or validate the inputs…
if (!$validationOK) {
echo “please check your details”;
header(“Location: http://my webpage/testform.php”);
} else {
// Here send the email out…
}
In this manner, if the validation is bad or false, it echos the message and loops back…
If the validation is NOT bad or NOT false, it then continues to send the email.

There are better ways to do this than using the REDIRECTION “header()” function. Most use a form that
just “posts” back to itself and if errors or bad input exists, it displays it above the input areas.

For validation, you can do it this way. (Just one example of one of your fields…)
$validationOK = true;
$Name = $_POST[“Name”];
if (!$isset($Name) OR $Name == “”) $validationOK = false; // Check if name is empty
if (!preg_match("/^[a-zA-Z0-9 .,]+$/", $Name)) $validationOK = false; // Only a-z, A-Z, 0-9 allowed

This checks to see if the name is not-set or empty and that only letters and numbers are allowed.

Hope that helps…

EDIT ADDED: Oh, you also do not need EXIT after a HEADER line. Does nothing…

Sponsor our Newsletter | Privacy Policy | Terms of Service