show previously selected value in SELECT box?
Moderator: General Moderators
show previously selected value in SELECT box?
Hi all
I have a form that accepts user input from a variety of TEXT and SELECT boxes and save the values in a mySQL table - OK so far.
I need to offer a way of subsequently enabling the user to update the values.
Text box values are no problem - but what is the best way of displaying a form with the values of SELECT boxes displayed?
maybe I'm missing something ...
thanks in advance for your time
I have a form that accepts user input from a variety of TEXT and SELECT boxes and save the values in a mySQL table - OK so far.
I need to offer a way of subsequently enabling the user to update the values.
Text box values are no problem - but what is the best way of displaying a form with the values of SELECT boxes displayed?
maybe I'm missing something ...
thanks in advance for your time
The option element having an attribute selected="selected" will be the ...err.. selected option 
e.g.will pre-select option #2 (instead of #1)
Your php script has to print the option tag you want to be selected with this attribute, e.g.
e.g.
Code: Select all
<html>
<head><title>...</title></head>
<body>
<form method="post" action="?">
<div>
<select name="xyz">
<option>1</option>
<option selected="selected">2</option>
<option>3</option>
</select>
<input type="submit" />
</div>
</form>
</body>
</html>Your php script has to print the option tag you want to be selected with this attribute, e.g.
Code: Select all
<html>
<head><title>...</title></head>
<body>
<form method="post" action="?">
<div>
<select name="xyz"><?php
for($i=0; $i<5; $i++) {
if ( $i==$selectedValue ) {
echo '<option selected="selected">', $i, '</option>';
}
else {
echo '<option>', $i, '</option>';
}
}
?></select>
<input type="submit" />
</div>
</form>
</body>
</html>- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
I made this simple function to call everytime I need it
Using it with your code you may script this
Is very simple but usefull. Ofcourse it could be writtend better 
Code: Select all
#############################################
### Returns SELECTED if option value == $_POST['value']
#
// $id_string -> (string)
// $id_posted -> (string)
function is_selected_option($id_string,$id_posted){
if($id_string == $id_posted){
return " selected ";
}
}Code: Select all
<html>
<head><title>...</title></head>
<body>
<form method="post" action="?">
<div>
<select name="xyz">
<option value="1" <?php echo is_selected_option("1",$_POST['xyz']);?>>1</option>
<option value="2" <?php echo is_selected_option("2",$_POST['xyz']);?>>2</option>
<option value="3" <?php echo is_selected_option("3",$_POST['xyz']);?>>3</option>
</select>
<input type="submit" />
</div>
</form>
</body>
</html>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
I'm forword improving all my script library because of that kind of stupid errors I left behind. I post this just to suggest deBassMan to start writing functions for this kind of common use piece of code so you don't have to write them again and again, and in cases like mine (mean bug fixing like the bug you notice) if you have a a code library you fix it one time only without loosing time looking for the mistake all over the code-set. Ass soon as you understand the need of abstractions, as less time you loose.Jcart wrote:Jaxolotl: your script will throw a notice if $_POST['xyz'] is not defined.
by the way thank you very much Jcart for your suggestion
- Jaxolotl
- Forum Contributor
- Posts: 137
- Joined: Mon Nov 13, 2006 4:19 am
- Location: Argentina and Italy
Think this is better or have other suggestions?
Code: Select all
<?php
#############################################
### Returns SELECTED if option value == $_POST['value']
#
// $value_string -> (string)
// $id_posted -> (string) THE KEY OF THE POST ARRAY we want to retrive value
function is_selected_option($value_string ,$id_posted){
if((isset($_POST[$id_posted]))&&($value_string == $_POST[$id_posted])){
return " selected ";
}
}
?>
<html>
<head><title>...</title></head>
<body>
<form method="post" action="?">
<div>
<select name="xyz" action="">
<option value="1" <?php echo is_selected_option("1","xyz")?>>1</option>
<option value="2" <?php echo is_selected_option("2","xyz")?>>2</option>
<option value="3" <?php echo is_selected_option("3","xyz")?>>3</option>
</select>
<input type="submit" />
</div>
</form>
</body>
</html>