PHP form validation and post sending
Posted: Thu Feb 11, 2010 4:34 pm
hi, I am very new to php and I need a little help from you.
On my side I want contact form, so I grab some validation and emaling codes and put them together with html.
Code post data from form to my email, but it not validating inputs realtime after pushing submit button. I do not know what I have done wrong please help me to find out ...
BTW, I want to add check box for desiding user, if he want to confirm a copy of form on his mail. How do I do that?
Here is code so far:
Boco
On my side I want contact form, so I grab some validation and emaling codes and put them together with html.
Code post data from form to my email, but it not validating inputs realtime after pushing submit button. I do not know what I have done wrong please help me to find out ...
BTW, I want to add check box for desiding user, if he want to confirm a copy of form on his mail. How do I do that?
Here is code so far:
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style>
.errText {
font-family: Arial;
font-size: 10px;
color: #CC0000;
text-decoration: none;
font-weight: normal;
}
body {
font-family: Arial;
font-size: 14px;
color: #ffffff;
font-weight: normal;
}
</style>
<title>CONTACT</title>
<?php
$errFirstName = "";
$errLastName = "";
$errCompanyName = "";
$errAddress = "";
$errPostalCode = "";
$errPhone = "";
$errFax = "";
$errEmail = "";
$errSubject = "";
$errComment = "";
if($_POST["contact1"]=="info"){
if(preg_match("/^[A-Z][a-zA-Z -']+$/", $_POST["firstname"]) === 0)
{$errFirstName = '<p class="errText">First Name must be from letters, dashes, spaces and must not start with dash</p>';
$error=true;
}
if(preg_match("/^[A-Z][a-zA-Z -']+$/", $_POST["lastname"]) === 0)
{$errLastName = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>';
$error=true;
}
if(preg_match("/^[A-Z][a-zA-Z -']+$/", $_POST["companyname"]) === 0)
{$errCompanyName = '<p class="errText">Company Name must be from letters, dashes, spaces and must not start with dash</p>';
$error=true;
}
if(preg_match("/^[a-zA-Z0-9 _.,:\"\']+$/", $_POST["address"]) === 0)
{$errAddress = '<p class="errText">Address must be only letters, numbers or one of the following ". , : /"</p>';
$error=true;
}
if(preg_match("/^\d{4}$/", $_POST["postalcode"]) === 0)
{$errPostalCode = '<p class="errText">Postal Code must be 4 digits</p>';
$error=true;
}
if(preg_match("/^\d{1}-\d{3}-\d{3}-\d{4}$/", $_POST["phone"]) === 0)
{$errPhone = '<p class="errText">Phone must comply with this mask: 1-333-333-4444</p>';
$error=true;
}
if(preg_match("/^\d{1}-\d{3}-\d{3}-\d{4}$/", $_POST["fax"]) === 0)
{$errFax = '<p class="errText">Fax must comply with this mask: 1-333-333-4444</p>';
$error=true;
}
if(preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $_POST["email"]) === 0)
{$errEmail = '<p class="errText">Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)</p>';
$error=true;
}
if(preg_match("/^[A-Z][a-zA-Z -']+$/", $_POST["subject"]) === 0)
{$errSubject = '<p class="errText">Subject must be from letters, dashes, spaces and must not start with dash</p>';
$error=true;
}
if(preg_match("/^[A-Z][a-zA-Z -']+$/", $_POST["comment"]) === 0)
{$errComment = '<p class="errText">Comment must be from letters, dashes, spaces and must not start with dash</p>';
$error=true;
}
?>
<?php
if ($_POST["contact1"]<>'' && $error==false) {
$ToEmail = 'my.3home.email@gmail.com';
$EmailSubject = 'Contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$mailheader .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$MESSAGE_BODY .= "FirstName: ".$_POST["firstname"]."<br>";
$MESSAGE_BODY .= "LastName: ".$_POST["lastname"]."<br>";
$MESSAGE_BODY .= "CompanyName: ".$_POST["companyname"]."<br>";
$MESSAGE_BODY .= "Address: ".$_POST["address"]."<br>";
$MESSAGE_BODY .= "PostalCode: ".$_POST["postalcode"]."<br>";
$MESSAGE_BODY .= "Phone: ".$_POST["phone"]."<br>";
$MESSAGE_BODY .= "Fax: ".$_POST["fax"]."<br>";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br>";
$MESSAGE_BODY .= "Subject: ".nl2br($_POST["subject"])."<br>";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br>";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("ERROR!!!");
}
?>
<center>
<h1 style="color:#ffa500">
<br>
<br>
THANK YOU! Your message is send!
<br>
our team
<br>
<br>
</h1>
<br>
<br>
<br>
<h2 style="color:#0088cc">
Click <a href="http://www.testarea.slowebdesigner.eu/testk2/contact1.php" target="_parent">here</a> for back.
</center>
<?php
}else {
?>
</head>
<body>
<center>
<form enctype="multipart/form-data" name="contact1" action="<?php $PHP_SELF ?>" method="POST">
<input type="hidden" name="contact1" value="info">
<table width="500" border="0" cellpadding="4" cellspacing="0" bordercolor="#000000" bgcolor="#ffa500">
<tr align="center">
<td colspan="2"><strong style="font-size:18px">CONTACT FORM</strong></td>
</tr>
<tr align="center" bgcolor="#0088cc">
<td colspan="2" bgcolor="#ffa500">Fields with * are required.</td>
</tr>
<tr>
<td width="85">First Name:</td>
<td>
<input name="firstname" type="text" size="55" maxlength="100" value="<?php echo $_POST["firstname"]; ?>"> *
<?php if(isset($errFirstName)) echo $errFirstName; ?>
</td>
</tr>
<tr>
<td>Last Name:</td>
<td>
<input name="lastname" type="text" size="55" maxlength="100" value="<?php echo $_POST["lastname"]; ?>">
<?php if(isset($errLastName)) echo $errLastName; ?>
</td>
</tr>
<tr>
<td>Company Name:</td>
<td>
<input name="companyname" type="text" size="55" maxlength="100" value="<?php echo $_POST["companyname"]; ?>">
<?php if(isset($errCompanyName)) echo $errCompanyName; ?>
</td>
</tr>
<tr>
<td>Address:</td>
<td>
<input name="address" type="text" size="55" maxlength="100" value="<?php echo $_POST["address"]; ?>">
<?php if(isset($errAddress)) echo $errAddress; ?>
</td>
</tr>
<tr>
<td>Postal Code:</td>
<td>
<input name="postalcode" type="text" size="12" maxlength="5" value="<?php echo $_POST["postalcode"]; ?>">
<?php if(isset($errPostalCode)) echo $errPostalcode; ?>
</td>
</tr>
<tr>
<td>Phone:</td>
<td>
<input name="phone" type="text" size="14" maxlength="15" value="<?php echo $_POST["phone"]; ?>">
<?php if(isset($errPhone)) echo $errPhone; ?>
</td>
</tr>
<tr>
<tr>
<td>Fax:</td>
<td>
<input name="fax" type="text" size="14" maxlength="15" value="<?php echo $_POST["fax"]; ?>">
<?php if(isset($errFax)) echo $errFax; ?>
</td>
</tr>
<tr>
<td>Email:</td>
<td>
<input name="email" type="text" size="55" maxlength="100" value="<?php echo $_POST["email"]; ?>"> *
<?php if(isset($errEmail)) echo $errEmail; ?>
</td>
</tr>
<tr>
<tr>
<td>Subject:</td>
<td>
<input name="subject" type="text" size="55" maxlength="100" value="<?php echo $_POST["subject"]; ?>"> *
<?php if(isset($errSubject)) echo $errSubject; ?>
</td>
</tr>
<tr>
<td>Comment:</td>
<td>
<textarea name="comment" id="comment" cols="42" rows="7" value="<?php echo $_POST["comment"]; ?>"></textarea> *
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="reset" style="color:#cc0000" value="Reset">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
<?php
};
?>