Link to home
Start Free TrialLog in
Avatar of sparky-nyc
sparky-nyc

asked on

How to use selectedIndex value in a hidden field?

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!

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 !!!
   include "footer.php";  //html footer stuff
}

... // other functions

// end of file.
?>


ASKER CERTIFIED SOLUTION
Avatar of pickett_richard
pickett_richard

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
    Accept: pickett_richard {http:#8223044}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

snoyes_jw
EE Cleanup Volunteer