Log in

View Full Version : PHP Form Validation



gold2040
05-28-2006, 04:51 PM
Hey

What's the easiest way of validating form fields by using PHP

Cheers

Tom Evans

Twey
05-28-2006, 05:13 PM
The equality operator.

pkcidstudio
06-01-2006, 06:27 PM
gold i have a good form validator tha ti created if you would like it

f472745
06-05-2006, 04:44 PM
pkcidstudio, may I have a copy of your form validation?

djr33
06-05-2006, 04:46 PM
This is an incredibly general question.

What are you verifying?

Basically...

<?php
if ($_GET['variable'] == "value") {
dostuff();
}
?>

or, perhaps,
<?php
if ($_GET['variable'] != "value") {
die();
}
?>


But what are you trying to accomplish?


Using the GET and POST methods of forms and the $_POST/$_GET arrays, you can get the values submitted by the form. Then you can use ifs, etc. to "verify" them.

f472745
06-05-2006, 05:51 PM
I have a registration form in which all the fields must be entered. If they are not entered a list of the fields not entered should be displayed.
Here is the coding I have without the form validation

<?php require_once('Connections/conJay.php'); ?>
<?php
// *** Redirect if username exists
$MM_flag="MM_insert";
if (isset($_POST[$MM_flag])) {
$MM_dupKeyRedirect="errorPage.html";
$loginUsername = $_POST['txtUserId'];
$LoginRS__query = "SELECT user_id FROM users WHERE user_id='" . $loginUsername
. "'";
mysql_select_db($database_conJay, $conJay);
$LoginRS=mysql_query($LoginRS__query, $conJay) or die(mysql_error());
$loginFoundUser = mysql_num_rows($LoginRS);

//if there is a row in the database, the username was found - can not add the
requested username
if($loginFoundUser){
$MM_qsChar = "?";
//append the username to the redirect page
if (substr_count($MM_dupKeyRedirect,"?") >=1) $MM_qsChar = "&";
$MM_dupKeyRedirect = $MM_dupKeyRedirect . $MM_qsChar
."requsername=".$loginUsername;
header ("Location: $MM_dupKeyRedirect");
exit;
}
}

function GetSQLValueString($theValue, $theType, $theDefinedValue = "",
$theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "formRegister")) {
$insertSQL = sprintf("INSERT INTO users (user_id, f_name, l_name, password,
security_answer, email, Staff_Key) VALUES (%s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['txtUserId'], "text"),
GetSQLValueString($_POST['txtFname'], "text"),
GetSQLValueString($_POST['txtLname'], "text"),
GetSQLValueString($_POST['txtPassword'], "text"),
GetSQLValueString($_POST['txtSecurity'], "text"),
GetSQLValueString($_POST['txtEmail'], "text"),
GetSQLValueString($_POST['txtKeyPhrase'], "text"));

mysql_select_db($database_conJay, $conJay);
$Result1 = mysql_query($insertSQL, $conJay) or die(mysql_error());

$insertGoTo = "registration_ok.html";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- TemplateBeginEditable name="doctitle" -->
<title>RegistrationForm</title>
<!-- TemplateEndEditable -->
<!-- TemplateBeginEditable name="head" --><!-- TemplateEndEditable -->
<style type="text/css">
<!--
.style9 {font-family: "Times New Roman", Times, serif}
.style10 {
font-family: "Times New Roman", Times, serif;
font-size: large;
font-weight: bold;
}
.style12 {font-size: large; font-weight: bold; }
.style14 {font-size: large}
.style15 {
font-size: xx-large;
font-family: "Times New Roman", Times, serif;
font-weight: bold;
}
.style19 {font-size: medium}
body {
background-image: url(images/center/pattern1.jpg);
background-repeat: repeat;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
}
-->
</style>
</head>

<body>
<form id="formRegister" name="formRegister" method="POST" action="<?php echo
$editFormAction; ?>">
<label>
<blockquote>
<p align="center"><label><span class="style15">Registration
Form</span></label>
<span class="style15">&nbsp;</span></p>
<p>&nbsp;</p>
<table width="498" height="332" border="1" align="center">
<tr>
<td width="295"><div align="right" class="style12"><span
class="style9">First Name</span></div></td>
<td width="193"><div align="center">
<input name="txtFname" type="text" id="txtFname" />
</div></td>
</tr>
<tr>
<td><div align="right" class="style12"><span class="style9">Last Name
</span></div></td>
<td><div align="center">
<input name="txtLname" type="text" id="txtLname" />
</div></td>
</tr>
<tr>
<td height="26"><div align="right" class="style12"><span
class="style9">Email </span></div></td>
<td><div align="center">
<input name="txtEmail" type="text" id="txtEmail" />
</div></td>
</tr>
<tr>
<td height="32"><span class="style10">
<label> </label>
</span> <span class="style12">
<label> </label>
</span> <span class="style12">
<label></label>
</span> <span class="style14"><strong>
<label></label>
</strong>
<label></label>
</span>
<div align="right" class="style12"><span class="style9">Create a User
ID</span></div>
</td>
<td><div align="center">
<input name="txtUserId" type="text" id="txtUserId" />
</div></td>
</tr>
<tr>
<td><div align="right" class="style12"><span class="style9">Password
</span></div></td>
<td><div align="center">
<input name="txtPassword" type="password" id="txtPassword" />
</div></td>
</tr>
<tr>
<td height="30"><div align="right" class="style12"><span
class="style9">Re-enter Password</span></div></td>
<td><div align="center">
<input name="txtPassword2" type="password" id="txtPassword2" />
</div></td>
</tr>
<tr>
<td height="40"><div align="right" class="style12">
<p class="style9">Mothers maiden name</p>
</div>

<div align="right"><span class="style19">used to retrieve lost
password</span></div></td>
<td><div align="center">
<input name="txtSecurity" type="text" id="txtSecurity" />
</div></td>
</tr>
<tr>
<td height="45">
<div align="center" class="style10">
<div align="right">Staff enter key phrase </div>
</div></td>
<td><label>
<div align="center">
<input name="txtKeyPhrase" type="text" id="txtKeyPhrase" />
</div>
</label></td>
</tr>
<tr>
<td><div align="center"></div></td>
<td><div align="center">
<p>
<input name="btnSubmit" type="submit" id="btnSubmit" value="Submit"
/>
<input name="btnReset" type="reset" id="btnReset" value="Reset" />
</p>
</div></td>
</tr>
</table>
<p>&nbsp;</p>
</blockquote>
</label>
<label>
<p>
<label></label>
</p>
<p>
</label>
<label>
</p>
<blockquote>&nbsp;</blockquote>
</label>
<label>
<blockquote>
<p>&nbsp;</p>
<p>&nbsp;</p>
</blockquote>
<input type="hidden" name="MM_insert" value="formRegister">
</label>
</form>
</body>
</html>

pkcidstudio
06-07-2006, 06:48 PM
sorry it took so long. hope this helps this will generate a error above the form


<?php

if (isset($_POST["submit"]))
{
$error = array();
$message = "";
$validEmail = "^[0-9a-z~!#$%$_-]([.]?[0-9a-z~!#$%$_-])*" . "@[0-9a-z~!#$%$_-]([.]?[0-9a-z~!#$%$_-])*" . ".[0-9a-z~!#$%$_-]([.]?[0-9a-z~!#$%$_-])*$";
$validName = "^[a-z]*" . " [a-z]*$";
$validZip = "^([0-9]{4,5})$";

if (!eregi($validName, $_POST["name"]))
{
$error[] = 'Name field incomplete, First and Last Please.';
}
if (!eregi($validEmail, $_POST["email"]))
{
$error[] = 'Email field incomplete.';
}

if ($_POST["street"] == "")
{
$error[] = 'Street field incomplete.';
}
if ($_POST["city"] == "")
{
$error[] = 'City field incomplete.';
}
if (!eregi($validZip, $_POST["zip]))
{
$error[] = 'Zip code field must be 4 to 5 digits in length.';
}

if (count($error) == 0)
{
require 'sendingemail.php';
print '<meta http-equiv="refresh" content="0;url=thankyou.php">';
exit;

}

}
?>

form


<h3>Send Us A Message:</h3>

<form method="post" action="">


<?php

if (count($error) > 0)
{
echo '<p>Following errors ocurred:</p>';

echo '<ul>';

foreach ($error as $fail)
{
echo '<li>'. $fail .'</li>'. "\n";
}

echo '</ul>';
}

?>

<!-- /////validation end////////validation end//////////validation end/////////validation end////// -->

<table width="100%" border="0" cellspacing="0" cellpadding="0" id="tabletimepadding">
<tr>
<td>Name: (First / Last)<br/>
<input name="name" type="text" value="<?php echo $_POST["name"]; ?>" class="feilds">
</td>
</tr>
<tr>
<td>E-mail:<br/>
<input name="email" id="email" value="<?php echo $_POST["email"]; ?>" type="text" class="feilds">
</td>
</tr>
<tr>
<td>Street:<br/>
<input name="address" id="address" value="<?php echo $_POST["address"]; ?>" type="text" class="feilds">
</td>
</tr>
<tr>
<td>

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>City:<br/>
<input name="city" id="city" value="<?php echo $_POST["city"]; ?>" type="text" class="city">
</td>
<td>State:<br/>
<select name="state" class="state">
<OPTION VALUE="--">--</OPTION>
<OPTION id=""state" value="AK">AK</OPTION>
<OPTION id="state" value="AL">AL</OPTION>
<OPTION id="state" value="AR">AR</OPTION>
<OPTION id="state" value="AZ">AZ</OPTION>
<OPTION id="state" value="CA">CA</OPTION>
<OPTION id="state" value="CO">CO</OPTION>
<OPTION id="state" value="CT">CT</OPTION>
<OPTION id="state" value="DC">DC</OPTION>
<OPTION id="state" value="DE">DE</OPTION>
<OPTION id="state" value="FL">FL</OPTION>
<OPTION id="state" value="GA">GA</OPTION>
<OPTION id="state" value="HI">HI</OPTION>
<OPTION id="state" value="IA">IA</OPTION>
<OPTION id="state" value="ID">ID</OPTION>
<OPTION id="state" value="IL">IL</OPTION>
<OPTION id="state" value="IN">IN</OPTION>
<OPTION id="state" value="KS">KS</OPTION>
<OPTION id="state" value="KY">KY</OPTION>
<OPTION id="state" value="LA">LA</OPTION>
<OPTION id="state" value="MA">MA</OPTION>
<OPTION id="state" value="MD">MD</OPTION>
<OPTION id="state" value="ME">ME</OPTION>
<OPTION id="state" value="MI">MI</OPTION>
<OPTION id="state" value="MN">MN</OPTION>
<OPTION id="state" value="MO">MO</OPTION>
<OPTION id="state" value="MS">MS</OPTION>
<OPTION id="state" value="MT">MT</OPTION>
<OPTION id="state" value="NC">NC</OPTION>
<OPTION id="state" value="ND">ND</OPTION>
<OPTION id="state" value="NE">NE</OPTION>
<OPTION id="state" value="NH">NH</OPTION>
<OPTION id="state" value="AK">NJ</OPTION>
<OPTION id="state" value="NM">NM</OPTION>
<OPTION id="state" value="NV">NV</OPTION>
<OPTION id="state" value="NY">NY</OPTION>
<OPTION id="state" value="OH">OH</OPTION>
<OPTION id="state" value="OK">OK</OPTION>
<OPTION id="state" value="OR">OR</OPTION>
<OPTION id="state" value="PA">PA</OPTION>
<OPTION id="state" value="RI">RI</OPTION>
<OPTION id="state" value="SC">SC</OPTION>
<OPTION id="state" value="SD">SD</OPTION>
<OPTION id="state" value="TN">TN</OPTION>
<OPTION id="state" value="TX">TX</OPTION>
<OPTION id="state" value="UT">UT</OPTION>
<OPTION id="state" value="VA">VA</OPTION>
<OPTION id="state" value="VT">VT</OPTION>
<OPTION id="state" value="WA">WA</OPTION>
<OPTION id="state" value="WI">WI</OPTION>
<OPTION id="state" value="WV">WV</OPTION>
<OPTION id="state" value="WY">WY</OPTION>
</SELECT>
</td>
<td>Zip:<br/>
<input name="zip" id="zip" value="<?php echo $_POST["zip"]; ?>" type="text" class="zip" maxlength="5">
</td>
</tr>
</table>


</td>
</tr>
<!-- <tr>
<td>

<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Phone:<br/>
<input name="phone" type="text" class="phone">

</td>
<td>
Mobile:<br/>
<input name="phone" type="text" class="phone2">

</td>
</tr>
</table>

</td>
</tr>-->
<tr>
<td>Subject:<br/>
<select name="subject">
<option id="subject" value="Customer Service">Customer Service</option>
<option id="subject" value="Product Information">Product Information</option>
<option id="subject" value="Web Site">Web Site</option>
<option id="subject" value="Product Testimonial*">Product Testimonial*</option>
<option id="subject" value="Address Change">Address Change</option>
<option id="subject" value=" Newsletter">Hardline Newsletter</option>
<option id="subject" value="Employment Opportunities">Employment Opportunities</option>
<option id="subject" value="Other">Other</option>
</select>
</td>
</tr>
<tr>
<td>Comments:<br/>
<textarea rows="25" class="bigfeilds" id="comments" name="abb7"></textarea>
</td>
</tr>
</table>
<input type="submit" name="submit" id="send" value="Send ›" />


</form>