Forum Moderators: coopster
Below is the form html
<input name="myemail" value="" size="24" tabindex="44">
<input name="firstname" value="" size="52" tabindex="46">
<input name="laststname" value="" size="52" tabindex="48">
<input name="street" size="52" tabindex="50">
<input name="city" size="30" tabindex="54">
<input name="state" size="6" tabindex="56">
<input name="zip" value="" size="10" tabindex="58">
<input name="day_phone" value="" size="38" tabindex="60">
<input name="evening_phone" tabindex="62" value="" size="38">
<select name="departure">
<option value="0">Departure City</option>
<?PHP // Generate a drop-down list of sections.
$result = $connector->query('SELECT ID,departure FROM departure ORDER BY departure');
while ($row = $connector->fetchArray($result)){
echo '<option value="'.$row['ID'].'">'.$row['departure'].'</option>';
}
?>
</select>
<select name="accomodationtype" size="1" tabindex="13">
<option value="All-Inclusive">All-Inclusive</option>
<option value="European Plan">European Plan</option>
<option value="No Preference">No Preference</option>
</select>
<select name="flexibility" size="1" tabindex="13">
<option value="Not">not flexible</option>
<option value="Somewhat">somewhat flexible</option>
<option value="Very">very flexible</option>
</select>
<select name="num_travelers" size="1" tabindex="20">
<option value="0">0</option>
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9 or more">9 or more</option>
</select>
<textarea name="comments" rows="8" cols="62" tabindex="64"></textarea>
<input type="submit" name="submitButtonName" value="Price my vacation!">
<modnote>I left example fields for each type to be validated but removed all html - jatar_k</modnote>
[edited by: jatar_k at 11:21 pm (utc) on Feb. 7, 2005]
quite the page here btw, I for one am not gonna write something to validate the page partially. If you just need to know if every element on the page was at least filled out/ checked, I could post you a function I found somewhere though
Basics of Submitting and Emailing Forms with PHP [webmasterworld.com]
message 3 talks about validation for a simple form, I know there is emailing after that but take a look at the validation there and see if that helps some.
Below is the code I posted to my server:
FORM - form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>test</title>
</head><body>
<form>
<p>Please fill in the following Information
<p><form name="greatpurchase" method="post" action="test.php">
<p>First Name <input type="text" name="firstname" size="20">
<p>Last Name <input type="text" name="lastname" size="20">
<p>Email Address <input type="text" name="email" size="50">
<p>Product <select name="product">
<option value="none">Select a Product</option>
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
<option value="product3">Product 3</option>
</select>
<p>Quantity <input type="text" name="qty" size="3">
<input type="submit" name="sendme" value="Send Order">
</form>
</body>
</html>
PROCESS PHP - test.php
<?php
$errmsg = "";
if (!isset($_POST['email']) ¦¦ empty($_POST['email'])) $errmsg .= "<p>Please enter your email address";
if (!isset($_POST['firstname']) ¦¦ empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name";
if (!isset($_POST['lastname']) ¦¦ empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name";
if (!isset($_POST['day_phone']) ¦¦ empty($_POST['day_phone'])) $errmsg .= "<p>Please enter your day phone";
if (!isset($_POST['evening_phone']) ¦¦ empty($_POST['evening_phone'])) $errmsg .= "<p>Please enter your evening phone";
if ($_POST['departure'] == "Departure City") $errmsg .= "<p>Please select a Departure City";
if ($_POST['destination'] == "Destination") $errmsg .= "<p>Please select a Destination";if ($errmsg == "") {
echo $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
} else {
echo "<p>success: all fields were filled out";
}
?>
<body>
<form>
<p>Please fill in the following Information
<p><form name="greatpurchase" method="post" action="test.php">
This change should get you moving forward again.
<body>
<p>Please fill in the following Information
<p><form name="greatpurchase" method="post" action="test.php">
Set $errmsg to NULL. I seem to get mixed results trying to set an empty string, or checking it later.
$errmsg = NULL;
if (!isset($_POST['email']) ...
Then I test $errmsg for NULL:
if ($errmsg!= NULL) {
echo $errmsg;
echo "<br><a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
} else {
echo "<p>success: all fields were filled out";
}
FWIW, you will always get an error message returned. That's because you are trying to validate non-existent form fields. But I assume you are just testing right now...
BTW - I'm using
PHP Version 4.3.10 on linux server
To be sure I'm using the correct code I'm posting it again:
//FORM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>order</title>
</head>
<body>
<p>Please fill in the following Information
<p><form name="greatpurchase" method="post" action="test.php">
<p>First Name <input type="text" name="firstname" size="20">
<p>Last Name <input type="text" name="lastname" size="20">
<p>Email Address <input type="text" name="email" size="50">
<p>Product <select name="product">
<option value="none">Select a Product</option>
<option value="product1">Product 1</option>
<option value="product2">Product 2</option>
<option value="product3">Product 3</option>
</select>
<p>Quantity <input type="text" name="qty" size="3">
<input type="submit" name="sendme" value="Send Order">
</form>
</body>
</html>
//VALIDATION CODE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>quote</title>
</head>
<body>
<?php
$errmsg = "";
if (!isset($_POST['firstname']) ¦¦ empty($_POST['firstname'])) $errmsg .= "<p>Please enter your first name</p>";
if (!isset($_POST['lastname']) ¦¦ empty($_POST['lastname'])) $errmsg .= "<p>Please enter your last name</p>";
if (!isset($_POST['email']) ¦¦ empty($_POST['email'])) $errmsg .= "<p>Please enter your email address</p>";
if (!isset($_POST['qty']) ¦¦ empty($_POST['qty'])) $errmsg .= "<p>Please enter the qantity you wish to order</p>";
if ($_POST['product'] == "none") $errmsg .= "<p>Please select the product you wish to order</p>";
if ($errmsg!= "") {
echo $errmsg;
echo "<a href=\"javascript:history.back();\">Please go back and fill out the missing fields</a>";
exit;
} else {
echo "<p>success: all fields were filled out</p>";
}
?>
</body>
</html>