All I want to do is to pass the selectedIndex value in a hidden field at the end of my script. I'm assigning the selected value to $selvalue, which I know is receiving the selectedIndex value (which I can see with the alert window), but $selvalue seems to be in a local scope. In other words, if I try printing $selvalue at the end of the script, it shows up as empty!
Also, I would like to use onchange, since there are other fields in the same form whose values are dependent on the first choice (color). In other words, if possible, I don't want to have to post, and then reload the page.
Can someone please tell me what I'm missing? Thanks!
Here's sample code:
<?PHP
// filename: myscript.php
function choosecolor()
{
include "header.php"; //html header stuff
echo "<form name=\"form1\" action=\"myscript.php\" method=\"post\">\n";
echo "<select name=\"selbox\" onchange=\"\$selvalue = this.options[this.selectedIndex].value;alert(\$selvalue);\" >";
echo " <option value=\"red\">Red</option>";
echo " <option value=\"green\">Green</option>";
echo " <option value=\"blue\">Blue</option>";
echo "</select>\n";
echo "<input type=\"hidden\" name=\"colorchoice\" value=\"$selvalue\">\n";
echo "<input type=\"submit\" value=\"next\">\n";
echo "</form>\n";
echo "\$selvalue = $selvalue\n"; // !!! is empty !!!
...
switch($selvalue)
{
case "red":
echo "my favorite color is red\n";
break;
case "green":
echo "my favorite color is green\n";
break;
case "blue":
echo "my favorite color is blue\n";
break;
}
...
include "footer.php"; //html footer stuff
}
... // other functions
// end of file.
?>