Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help with required field

Status
Not open for further replies.

mrsbean

Technical User
Jul 14, 2004
203
US
I'm not a PHP programmer, but I'm trying to learn it.

Code:
<?PHP
include "config.php";
include "opendb.php";
$query="select trKey, trStartText, trWho, trType from training";

$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

?>
<table border = 1 cellpadding =10 align = "center"><tr><td align = "center"><font size = '4'>Select<br>This Slot</font></td><td><font size = '4'>Time Slot</td><td><font size='4'>Who</font></td><td><font size='4'>Needs Help With</font></td></tr>
<!-- Start of FORM -->
<form method="POST" action="select.php">
<input type=hidden name="required_fields" value="trKeys">

<?
$i=0;
while ($i < $num) {

$trkey=mysql_result($result, $i, "trkey");
$slot=mysql_result($result, $i, "trStartText");
$who=mysql_result($result,$i,"trWho");
$helpwith=mysql_result($result,$i,"trType");

echo "<tr><td><input type='radio' name='trKeys' value='$trkey'></td><td valign = 'top'>$slot</td><td valign = 'top'>$who &nbsp;</td><td>$helpwith &nbsp; </td></tr>";

$i++;
}

?>
<tr><Td>&nbsp; </td><td colspan = 3><input type="Submit" value="Submit"></td></tr>
</form>
<!-- End of FORM -->

The problem with the above code is that nothing happens when the user forgets to select a radio button for the trKeys field. The form submits and goes to the next page (select.php) where it returns an error because the value of trKeys was null.

All help is much appreciated.
 
I'm not sure what you need your form for. There are different ways to deal with this:

- Maybe you should try a form validator like there a thousands written in Java-Script.

- If this is not what you can go along with, you should declare one (the first) radio button as selected. So the error will not be a problem anymore.

- You could also put a validation in your select.php if $trKeys has a value or not and if not show a "not all filled in" - message and don't let the rest of code be executed



I would prefer the first one for the users convenience

If you want to get deeper into it I can remember there is a PEAR modul that does kind of a form validation
 
Here's another strategy:

Have the receiving PHP script validate the required fields as suggested. If something is missing redraw the form with the entered values preserved and the missing entry highlighted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top