Forum Moderators: coopster

Message Too Old, No Replies

Form Validation Help

         

thx967

10:19 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



I'm kind of new to php and have seen a bunch of solutions for form validation posted to forums. Unfortunatly - I haven't been able to get any of them to work properly. Can anybody help a newbie thru the steps?

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]

dmmh

10:46 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



seriously...what does need to be validated? theres so many elements....do they all need to be validated, most some be filled, are some optional etc etc

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

jatar_k

11:25 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld thx967,

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.

thx967

11:09 pm on Feb 8, 2005 (gmt 0)

10+ Year Member



thanks jatar_k for your post - I tried using the code you posted on the other forum topic and can't get it to work properly. The validation specifically doesn't work. Did I miss something?

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";
}
?>

grandpa

11:54 pm on Feb 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have two forms defined on this page, the first is incomplete.

<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">

thx967

1:58 am on Feb 9, 2005 (gmt 0)

10+ Year Member



thanks grandpa - I missed that one.

After that fix - I'm still getting no validation. The message I get returned without entering anything into the form is:

success: all fields were filled out

grandpa

3:51 am on Feb 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It works for me with the following mods:

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...

jatar_k

6:27 am on Feb 9, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



nasty typo I seem to have made in that thread.

The first time I wrote it in that post I put!= and the second time it says ==

I fixed it now in the original post, sorry about that and good catch, thanks.

thx967

9:22 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



Ok - I've been testing the validation portion of jatar_k revised file above on the other forum topic and stil get the same result. Starting to wonder if I'm crazy - I still get the same echo no matter how many/few fields I fill out = "success: all fields were filled out".

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>

thx967

9:27 pm on Feb 9, 2005 (gmt 0)

10+ Year Member



WOOOPS - Seeems I spoke to soon - upon refresh it worked this time!