Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

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.
?>


0
sparky-nyc
Asked:
sparky-nyc
1 Solution
 
pickett_richardCommented:
You're trying to use a bit of JavaScript in the onChange event to change a PHP variable ($selvalue). All PHP code is executed by the server before it is passed to the client browser. Therefore the $selvalue variable does not exist when the page has already loaded - this is why JavaScript cannot access PHP code/variables.

What you can do, however, is submit the form and then using PHP, retrieve the submitted value from selbox. Here is an example of your code with these slight modifications:

<?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\">";
  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=\"$_POST['selbox']\">\n";  // if form has been posted, this field contains the selected value of selbox
  echo "<input type=\"submit\" value=\"next\">\n";
  echo "</form>\n";
  echo "\selected value = $_POST["selbox"]\n";    // if form has been submitted, the selected value of selbox appears here
  include "footer.php";  //html footer stuff
}

... // other functions

// end of file.
?>

If you select a value from the drop-down box, then submit the form, the same page will reload but the hidden field will contain the value you selected and the text at the bottom will read 'selected value = ...' (where ... is the value you selected).

I only modified a couple parts in your code so it would print the selected value. There are more things you should do with your form code for better functionality so if you want to know more, just ask me.

Also, if you do want your page to update the value immediately (it depends what your page is for), you can do it with JavaScript by printing text inside a layer (<span> or <div> tag). But once again, it depends what your page will be for as to which option you choose. Personally, I prefer to do most things with PHP as it's server-side processing as opposed to client-side.

Hope that helps,
Richard
0
 
snoyes_jwCommented:
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
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now