Jump to content

Drop down box question?


garyed

Recommended Posts

Is it possible to keep the selected drop down box choice on the screen after submitting it on a form?

Here's a sample code, if I choose medium the correct number will be shown but the selection will immediately return

to high. I don't have this problem in javascript but I'd like to code this in php but i have a lot of drop down boxes.

<html>
<head></head>
<body>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="rpms"> 
<option name="high" value="1200"> high</option>
<option name=medium value="1000">medium</option>
<option name="low" value="825"> low </option>
<br>
<input type="submit" value="Submit"> 
<?php
$speed = $_GET[rpms];
?>
</form>
<div id="new_speed"> <?php echo ($speed); ?> </div>  
</body></html>

Link to comment
Share on other sites

isn't there a 'selected' attribute or something that can be set?

 

Remember that everything has normally been done to the page once you have processed your PHP code, so the second time through the program (after you have submitted the form) you will probably have to change the option code to put(add) the selected attribute against the one they selected on the first time through. Does that make sense?

Link to comment
Share on other sites

Easier to show it than explain what I mean for this.

 

<html>
<head></head>
<body>
<?php
$speed = $_GET[rpms];
$drop=array(
'1200' =>'<option name="high" value="1200"> high</option>
<option name=medium value="1000">medium</option>
<option name="low" value="825"> low </option>',
'1000' => '<option name=medium value="1000">medium</option>
<option name="low" value="825"> low </option>
<option name="high" value="1200"> high</option>',
'825' =>'<option name="low" value="825"> low </option>
<option name=medium value="1000">medium</option>
<option name="high" value="1200"> high</option>'
);
?>
<form method="get" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<select name="rpms">
<?php
if(isset($_GET['rpms']))
{echo "{$drop[$speed]}";}
else{
echo '<option name="high" value="1200"> high</option>
<option name=medium value="1000">medium</option>
<option name="low" value="825"> low </option>';
}
?>
<br>
<input type="submit" value="Submit">
</form>
<div id="new_speed"> <?php echo ($speed); ?> </div>
</body></html>

 

 

HTH

Teamatomic

Link to comment
Share on other sites

Wow, thanks Teamatomic

 

That works perfect,

I almost understand it but I'm not sure what the " => " means in the array.

I see that the order of the boxes follow the numbers in the array.

I'm obviously not too sharp on this stuff yet.

Link to comment
Share on other sites

Well I should have stated that I have about 10 drop boxes each with about 25 choices on the form I'm working on

so that might be a little impractical to code it that way.

It looks like I might have to stick to javascript for this one.

Let's see if I understand it right;  php works from the server so after a form is submitted & it does its thing with the variables it reloads the page & that is why the drop boxes will go back to their default settings.

It sounds like that would be an interesting function to figure out but its definitely beyond me, at least for now. 

Link to comment
Share on other sites

I did that to show you how it works. You would only need to build an array for each group once. Then to display it use the Submited as a key, just as we do now. the but is that you would then build the array like this

 

$select=array(

'rpms' =>array(

'1200' => '<option name="high" value="1200"> high</option>',

'1000' => '<option name=medium value="1000">medium</option>',

'825' => '<option name="low" value="825"> low </option>'),

'speed trap' = array(

'Car 1' => ' 3rd and Main St',

'Car 2' => 'Eight Mile and Gratiot',

'Car 2' => 'Red Light District')

); 

 

Then build the selects

 

echo "{$select[$group][$rpms]";

foreach($select[$group] as $key => $value)

{

if($key != $rpms){echo "$value";}

}

 

It sounds a bit complicated and it sorta is. But... 

this is a good time to use a variable variable. study this. Put it in a test page and call it to see what group and rpms output.

$group='rpms';
$$group='1000';
echo "$group";
echo '<br>';
echo "$rpms";

it will suit the selection of the proper group and the value associated with it quit well.

 

 

HTH

Teamatomic

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.