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>
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>