?
Solved

How to use selectedIndex value in a hidden field?

Posted on 2003-03-27
3
Medium Priority
?
252 Views
Last Modified: 2008-03-10
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
Comment
Question by:sparky-nyc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
pickett_richard earned 300 total points
ID: 8223044
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
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 11942724
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question