Forum Moderators: coopster

Message Too Old, No Replies

form - verify - drop menu

how to verify a drop down menu in php

         

mysterio2099

4:39 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I have a form in HTML and a verification php file to check all required fields. Everything is perfect, except I don't know how to check drop menus (state, and topic).

I'm using this code for the other fields:


// CHECKS TO SEE IF THE $FIRSTNAME VARIABLE IS VALID
if (!ereg("^[A-Za-z]{1,}$",$firstname))
{
echo ("<center>You entered an invalid <font color=\"#FF0000\"><b>FirstName</b></font>.</center>\n");
echo ("<br /><br />");
$validated = false;
}

I'm not sure if it only requires a slight change to use for the drop menus or not?

Please help, thanks

vincevincevince

5:40 pm on Jul 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, turn register_globals to off

Second, use $_GET['$firstname']; not just $firstname;

Finally, there is no need for any change to your code, just put the new form field name in its place.

mysterio2099

5:56 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



There's no need for the GET thing you said, because the code works perfect as it is. I just need to know how to make a validation code for drop menus. That code I showed was for a text field.

[edited by: jatar_k at 10:44 pm (utc) on July 25, 2005]

hughie

8:26 pm on Jul 25, 2005 (gmt 0)

10+ Year Member



I think he was suggesting that your methods are a bit risky as you are running with register globals on which isn't a very safe way to run PHP.

The code will work fine checking the value of the dropdown, but it won't return it to it's previous position.

to do that i use this function

function sort_selectboxes($name,$value)
{
if ($_REQUEST[$name]==$value)
{
return ' selected';
}
else
{
return '';
}
}

$enqchecked[0]=sort_selectboxes('select1','apples');
$enqchecked[1]=sort_selectboxes('select1','oranges');

$text.='<select name="select1">
<option value="apples" '.$enqchecked[0].'>apples</option>
<option value="oranges" '.$enqchecked[1].'>oranges</option>

hope that helps

Hughie