I have form and processor on one page. It shows errors for fields not filled in. Having trouble with the visitorcode input. I highlight the components that may be relevent in the code. Can also view on line at Test Link
Form will not accept visitorcode and clear error to present thanku.html
Any direction would be great?
<?php
/////////// LINK FORM AND MAIL PROCCESOR (ALL ON ONE PAGE) VALIDATION STARTS HERE ////////////////////
$errors = array();
function validate_form_items()
{
$form_items = array(
"name" => array(
"regex" => "/^([a-zA-Z '-]+)$/",
"error" => "<i> Appears in an improper format!</i>",
),
"email" => array(
"regex" =>
"/^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$/",
"error" => "<i> Enter a valid Email address!</i>",
),
[COLOR="Red"] "visitorcode" => array(
"" =>"/^([a-zA-Z '-]+)$/",
"error" => "<i> Please enter a correct Validate Code!</i>",
),[/COLOR]
);
global $errors;
if(!preg_match($form_items["name"]["regex"], $_POST["your_name"]))
$errors[] = "<b>Your Name:</b> ".$form_items["name"]["error"];
if(!preg_match($form_items["email"]["regex"], $_POST["your_email"]))
$errors[] = "<b>Your Email:</b> ".$form_items["email"]["error"];
if(!preg_match($form_items["email"]["regex"], $_POST["friend_email1"]))
$errors[] = "<b>Friend 1 Email:</b> ".$form_items["email"]["error"];
if(trim($_POST["friend_email2"]) != "")
{if(!preg_match($form_items["email"]["regex"], $_POST["friend_email2"]))
$errors[] = "<b>Friend 2 Email:</b> ".$form_items["email"]["error"];}
if(trim($_POST["friend_email3"]) != "")
{if(!preg_match($form_items["email"]["regex"], $_POST["friend_email3"]))
$errors[] = "<b>Friend 3 Email:</b> ".$form_items["email"]["error"];}
[COLOR="Red"]if (md5(trim($_POST[$visitorcode]))!=$_SESSION['image_random_value'])
$errors[] = "<b>Validate Code:</b> ".$form_items["visitorcode"]["error"];[/COLOR]
return count($errors);
}
/////////FORM MAIL STARTS HERE//////////////////////////////////////////////////////////////////////////////////
function email($from, $from_name, $to, $message)
{
//header("Location: thanku.html");return;
$headers .= "From: ".$from."\\r\
";
$headers .= "Content-type: text/plain; charset=ISO-8859-1";
$domian_name = "www.domain.net";
$subject = $from_name."- An invitation to view $domian_name";
$your_message = "Hi!\\r\
";
$your_message.= ucfirst($from_name);
$your_message.= "would like you to check out this website at $domian_name\\r\
";
$message=$your_message.stripslashes($message);
if (mail($to,$subject,$message,$headers) ) {
return true;
} else {
return false;
}
}
function print_error($errors)
{foreach($errors as $error)
{$err.=$error."<br/>";}
echo "<div style=\\"font-size:14px; font-weight:normal; color:red;\\"><span style=\\"background-color:yellow\\">$err</span><div>";
}
//////////////POSTING AND THANK YOU STARTS HERE///////////////////////////////////////////////////////////////////////////
function form_process()
{
[COLOR="Red"]$visitorcode = $_POST['visitorcode'];[/COLOR]//verification code
$from_name = $_POST["your_name"];
$fromyour_email = $_POST["your_email"];
$to = $_POST["your_email"].",".$_POST["friend_email1"].",".$_POST["friend_email2"].",".$_POST["friend_email3"];
//$message = $_POST["message"];
$error_count = validate_form_items();
if($error_count == 0)
{if(email($fromyour_email, $from_name, $to, $message)) header("Location: thanku.html");
else
{global $errors; $errors[] = "Email could not be send at this time. <br />Please report this to the Webmaster.";}}
}
if(isset($_POST["submit"])) form_process();
?>
<html><title>Link to my Friend/s</title>
<head>
<style type="text/css">
fieldset{width:315px;height:450px;background-color:#f1f1f1;}
legend{margin-left:31px;font-family:verdana;font-size:1.45em;font-weight:bold;color:#ff0000;}
label{float:left;width:110px;font-family:verdana;font-size:0.775em;color:#0000ff;}
#textfield{width:180px;margin-bottom:5px;}
#submit{margin-left:10px;margin-top:15px;width:330px;}
br{clear:left;}
</style>
</head>
<body>
<fieldset>
<legend>Link to my Friend/s.</legend><br />
Install your friend/'s email details to forward link to this website.<br /><br />
<i>Required *</i>
<form id="link" method="post" action="<?php echo $PHP_SELF?>" >
<div>[COLOR="Red"]<?php global $errors; if(count($errors) != 0){ print_error($errors);} ?>[/COLOR]</div>
<label for="your_name">My Name:*</label><input type="text" name="your_name" id="textfield" value="<?php echo $_POST["your_name"]?>" /><br />
<label for="your_email">My Email:*</label><input type="text" name="your_email" id="textfield" value="<?php echo $_POST["your_email"]?>" /><br /><br />
<label for="friend_email1">Friend's Email:*</label><input type="text" name="friend_email1" id="textfield" value="<?php echo $_POST["friend_email1"]?>" />1<br />
<i>Optional</i><br />
<label for="friend_email2">Friend's Email:</label><input type="text" name="friend_email2" id="textfield" value="<?php echo $_POST["friend_email2"]?>" />2<br />
<label for="friend_email3">Friend's Email:</label><input type="text" name="friend_email3" id="textfield" value="<?php echo $_POST["friend_email3"]?>" />3<br /><br />
<div>
<label>[COLOR="DarkGreen"]Validate Form:</label> <img src="randomImage.php"/>
<a href><img src="refresh.jpg" img style="border:0;" onclick="history.go()" alt="Get new Code" /></a>
<input type="text" name="visitorcode" size="5" />[/COLOR]
</div>
<br />
<label>Send Link:</label><input type="submit" name="submit" value="Send Website Link" />
</form>
</fieldset>
</body></html>