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!

returning option variable 1

Status
Not open for further replies.

rouse01

IS-IT--Management
Sep 10, 2001
143
US
A form I'm working on requires an option box which concatenates variables from a 2 dimensional array into the select area. When a user selects from the list, I need to hold onto only the second part of the concatenation (Vopt[x][2]). In my example below, I'm trying to trap the value in Topt. If you paste the code below as 'option.php', maybe you can tell me where I've bonked.

Code:
<html>
<head>
<title>Option Menu</title>
</head>
<body>
<?php
$Vopt[0][1]="I" ;
$Vopt[0][2]="0,2" ;
$Vopt[1][1]="You" ;
$Vopt[1][2]="1,2" ;
$Vopt[2][1]="He/She/It" ;
$Vopt[2][2]="2,2" ;
$Vopt[3][1]="We" ;
$Vopt[3][2]="3,2" ;
$Vopt[4][1]="All You all" ;
$Vopt[4][2]="4,2" ;
$Vopt[5][1]="They" ;
$Vopt[5][2]="5,2" ;
Echo "<br> OPT: ".$_POST[opt]."<br>\n" ;
Echo "<br> TOPT: ".$_POST[topt]."<br>\n" ;
?>
<form action="option.php" method="POST">
<Select Name="opt">
<?php 
for ($i = 0; $i <6; $i++)
{
if ( $_POST[opt] == $Vopt[$i][1]." - ".$Vopt[$i][2] )
	{
	echo "<option selected>", $Vopt[$i][1]." - ".$Vopt[$i][2], "</option>\n";
	echo "<input type='hidden' name='topt' value=".'"'.$Vopt[$i][2].'"'.">\n";
	}
else
	{
	echo "<option>", $Vopt[$i][1]." - ".$Vopt[$i][2], "</option>\n";
	echo "<input type='hidden' name='opt' value=".'"'.$Vopt[$i][2].'"'.">\n";
	}
}
?>
</select>
<INPUT TYPE=SUBMIT NAME=SUBMIT VALUE="Display">
</FORM>
</body>
</html>

Thanks - Keith
 
Sometimes the soltion is quite simple:
The <option> tag has a value attribute which defaults to the contained text if not set. All you need to do is print the desired value inside the value attribute:
Code:
echo '<option value="'.$Vopt[$i][2].'">', $Vopt[$i][1]." - ".$Vopt[$i][2], "</option>\n";
That's it.
 
Thanks DrJ - that's exactly what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top