Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form to email validation

Status
Not open for further replies.

wallm

Programmer
Apr 4, 2005
69
GB
Hi, I'm am absolute novice at PHP.
I want to put some PHP server side validation in the code for Page 2 so that a valid email must be entered, and so that the fields aren't empty. If they are then a message will display saying something like 'please try again'.

Can anyone help with the code?


Page 1

<form action="storage/FormToEmail.php" method="post" style="margin:5px;">
Name:<br /><input name="Name" type="text" id="Name" value="Your name">
<br />
Email:<br /><input name="Email" type="text" id="Email" value="Your email">
<br />
Comment:<br /><textarea name="Comments" cols="22" rows="4" id="Comments">Enter your message here to contact us</textarea>
<br />
<input type="submit">
</form>


Page 2

<?php


$my_email = "me@mywebsite.com";
if ($_SERVER['REQUEST_METHOD'] != "POST"){exit;}

$disallowed_name = array(':',';',"'",'"','=','(',')','{','}','@');

foreach($disallowed_name as $value)
{

if(stristr($_POST[Name],$value)){header("location: $_SERVER[HTTP_REFERER]");exit;}

}

$disallowed_email = array(':',';',"'",'"','=','(',')','{','}');

foreach($disallowed_email as $value)
{

if(stristr($_POST,$value)){header("location: $_SERVER[HTTP_REFERER]");exit;}

}

$message = "";

while(list($key,$value) = each($_POST)){if(!(empty($value))){$set=1;}$message = $message . "$key: $value\n\n";} if($set!==1){header("location: $_SERVER[HTTP_REFERER]");exit;}

$message = $message . "";
$message = stripslashes($message);

$subject = "Visitor Comments";
$headers = "From: " . $_POST['Email'] . "\n" . "Return-Path: " . $_POST['Email'] . "\n" . "Reply-To: " . $_POST['Email'] . "\n";

mail($my_email,$subject,$message,$headers);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="includes/stylesheet.css" rel="stylesheet" type="text/css">
</head>
<body>

<object>
<h1>Thank you <?php print stripslashes($_POST['Name']); ?>.</h1>
<h2>Your message has been sent and will be replied to if necessary.</h2>
</object>

</body>
</html>
 
If you are going to want to give your users a meaningful error message and a chance to correct their input, I strongly recommend that you combine the HTML and script into one script.

Generally in situations like this, I use a script of the form:

Code:
<?php

if (/* fields from the form this script produces were submitted */)
{
   //validate input

   if (/* input validated correctly */)
   {
      //tell the user so and/or send the user
      //to another script.
   }
   else
   {
      //output form again, with error messages and with
      //fields pre-populated with the previously-input
      //values
   }
}
else
{
   //output blank form
}
?>


As far as validating an email address goes, you might check out faq434-2408 .



Want the best answers? Ask the best questions! TANSTAAFL!
 
thank you.. that's very helpful.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top