Hi,
just been putting together a form and need some help with getting the error messages to work.
the email validation works ok, but i would like to make other fields a required input (i.e. name and address)...i don't know if i'm missing something obvious(i'm new to PHP) but i just can't get it to work...here's the code...
thanks for any help
<?
function check_len(&$check, $field, $max, &$err_field, $err="", $min, $min_err="")
{
if (strlen($field) > $max)
{
if ($err == "")
{
$err = $msg->err_maxlen($max);
}
$err_field = $err;
if ($check==true) $check = false;
}
if (strlen($field) < $min)
{
if ($min_err == "")
{
$min_err = $msg->err_minlen($min);
}
$err_field = $min_err;
if ($check==true) $check = false;
}
}
function check_mail(&$check, $fld, &$error_field, $invalidchars="", $blanks="")
{
global $msg;
$expr = "[_.0-9a-z-]+@([0-9a-z][0-9a-z-]+.)+[a-z][a-z]+\$";
//echo "expr=" . $expr . "<br>";
if ((!$fld) || ($fld=="") || (!eregi($expr,$fld)))
{
if ($invalidchars > "")
{
$error_field = $invalidchars . "<br>\n";
} else {
$error_field = "invalid characters<br>\n";
}
if ($check==true) $check =false;
}
if (strrpos($fld,' ') > 0)
{
if ($blanks > "")
{
$error_field = $blanks . "<br>\n";
} else {
$error_field = "blanks in e-mail<br>\n";
}
if ($check==true) $check =false;
}
}
if (empty($HTTP_POST_VARS["name"])) $HTTP_POST_VARS["name"]="";
if (empty($err_name)) $err_name=" ";
if (empty($HTTP_POST_VARS["email"])) $HTTP_POST_VARS["email"]="";
if (empty($err_email)) $err_email=" ";
if (empty($HTTP_POST_VARS["address"])) $HTTP_POST_VARS["address"]="";
if (empty($err_address)) $err_address=" ";
$checked = true;
if (isset($HTTP_POST_VARS["submit"]))
{
check_len($checked, $HTTP_POST_VARS["name"],30,$err_name,"Field too long! Maximum:30","Field required! Minimum:1");
check_len($checked, $HTTP_POST_VARS["email"],80,$err_email,"Field too long! Maximum:80","Field required! Minimum:1");
check_mail($checked, $HTTP_POST_VARS["email"], $err_email,"The form contains invalid characters or is incomplete.", "The e-mail contains blanks.");
check_len($checked, $HTTP_POST_VARS["address"],200,$err_address,"Field too long! Maximum:200","Field required! Minimum:5");
}
if ( empty($HTTP_POST_VARS["submit"]) or (!$checked) )
{
?>
<form action="<? echo $GLOBALS["PHP_SELF"] ?>" method="post">
<fieldset class="fieldset">
<legend>
<h3>PRODUCT ENQUIRY FORM</h3>
</legend>
<? echo $err_name ?>
<? echo $err_email ?> <? echo $err_comment ?>
<br />
<label for="name">Name:</label>
<input type="text" name="name" class="textfield" id="name" value="<? echo $HTTP_POST_VARS["name"] ?>" />
<br />
<label for="address">Address:</label>
<input type="text" name="address" class="textfield" id="address" value="<? echo $HTTP_POST_VARS["address"] ?>" />
<br />
<label for="email">Email:</label>
<input type="text" name="email" class="textfield" id="email" value="<? echo $HTTP_POST_VARS["email"] ?>" />
<br />
<label for="phone">Telephone:</label>
<input type="text" name="phone" class="textfield" id="phone" value="<? echo $HTTP_POST_VARS["phone"] ?>" />
<br />
<br />
<label for="caliper">Caliper:</label>
<input type="text" name="caliper" class="textfield" id="caliper" value="<? echo $HTTP_POST_VARS["caliper"] ?>" />
<br />
<label for="materials"> Materials:</label>
<select name="materials" id="materials" class="select">
<option>APET</option>
<option>GAG</option>
<option>PETG</option>
<option>PVC</option>
<option>Polypropolyne</option>
<? echo $HTTP_POST_VARS["materials"]?>
</select>
<br />
<label for="style">Style: (i.e. tuck ends)</label>
<input type="text" name="style" class="textfield" id="style" value="<? echo $HTTP_POST_VARS["style"] ?>" />
<br />
<label for="size">Size:</label>
<input type="text" name="size" class="textfield" id="size" value="<? echo $HTTP_POST_VARS["size"] ?>" />
<br />
<label for="quantity">Quantity:</label>
<input type="text" name="quantity" class="textfield" id="quantity" value="<? echo $HTTP_POST_VARS["quantity"] ?>" />
<br />
<br />
<label for="decoration"> Decoration:</label>
<select name="decoration" id="decoration" class="select">
<option>Plain</option>
<option>Printed</option>
<? echo $HTTP_POST_VARS["decoration"]?>
</select>
<br />
<label for="printing">Printing Type:</label>
<select name="printing" id="printing" class="select">
<option>Screen</option>
<option>Litho</option>
<? echo $HTTP_POST_VARS["printing"]?>
</select>
<br />
<label for="colours">No. of Colours:</label>
<input type="text" name="qcolours" class="textfield" id="colours" value="<? echo $HTTP_POST_VARS["colours"] ?>" />
<br />
<label for="delivery">Delivery Address:</label>
<textarea cols="30" rows="4" name="delivery" id="delivery" class="textarea"><? echo $HTTP_POST_VARS["delivery"]?>
</textarea>
<br />
<label for="samples">Samples Required:</label>
<select name="samples" id="samples" class="select">
<option>Yes</option>
<option>No</option>
<? echo $HTTP_POST_VARS["samples"]?>
</select>
<br />
<label for="comments">Additional Comments:</label>
<textarea cols="30" rows="4" name="comments" id="comments" class="textarea"><? echo $HTTP_POST_VARS["comments"]?>
</textarea>
<br />
<label for="submit"> </label>
<br class="nobr" />
<input name="submit" type="submit" class="submit" id="submit" value="send" />
<br />
</fieldset>
</form>
<br />
<?
}
if (isset($HTTP_POST_VARS["submit"]) and ($checked) ) {
$msg = "Your Message\n";
$msg .= "Name :".$HTTP_POST_VARS["name"]."\n";
$msg .= "Address :".$HTTP_POST_VARS["address"]."\n";
$msg .= "Email :".$HTTP_POST_VARS["email"]."\n";
$msg .= "Telephone :".$HTTP_POST_VARS["phone"]."\n";
$msg .= "Caliper :".$HTTP_POST_VARS["caliper"]."\n";
$msg .= "Materials :".$HTTP_POST_VARS["materials"]."\n";
$msg .= "Style :".$HTTP_POST_VARS["style"]."\n";
$msg .= "Size :".$HTTP_POST_VARS["size"]."\n";
$msg .= "Quantity :".$HTTP_POST_VARS["quantity"]."\n";
$msg .= "Decoration :".$HTTP_POST_VARS["decoration"]."\n";
$msg .= "Printing :".$HTTP_POST_VARS["printing"]."\n";
$msg .= "Colours :".$HTTP_POST_VARS["colours"]."\n";
$msg .= "Delivery :".$HTTP_POST_VARS["delivery"]."\n";
$msg .= "Samples :".$HTTP_POST_VARS["samples"]."\n";
$msg .= "Comments :".$HTTP_POST_VARS["comments"]."\n";
mail("email address","You have mail",
$msg);
echo "Your message has been sent to: <b>company name</b></ br></ br>\n";
echo nl2br($msg) . "</ br>\r";
}
?>