Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 1st, 2005, 09:00 PM
Authorized User
 
Join Date: Dec 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default Setting default for select (drop-down) box

I've been trying to figure out how to specify the default value that will appear in a select (drop-down) box that I am using to get a value.

For example:

<select name='color'>
<option value='RED'>Red</option>
<option value='YEL'>Yellow</option>
<option value='GRN'>Green</option>
<option value='BLU'>Blue</option>
<option value='WHT'>White</option>
<option value='BLK'>Black</option>
<option value='BRN'>Brown</option>
</select>

This will always default to RED regardless of the value of $color.
The only way I could figure my way out of this was to test the values of $color and set $default_xxx = "selected". For example:

if ($color == "RED") $default_RED = "selected";
if ($color == "YEL") $default_YEL = "selected";
if ($color == "GRN") $default_GRN = "selected";
if ($color == "BLU") $default_BLU = "selected";
if ($color == "WHT") $default_WHT = "selected";
if ($color == "BLK") $default_BLK = "selected";
if ($color == "BRN") $default_BRN = "selected";

Then re-write my select code:

<select name='color'>
<option value='RED' $default_RED>Red</option>
<option value='YEL' $default_YEL>Yellow</option>
<option value='GRN' $default_GRN>Green</option>
<option value='BLU' $default_BLU>Blue</option>
<option value='WHT' $default_WHT>White</option>
<option value='BLK' $default_BLK>Black</option>
<option value='BRN' $default_BRN>Brown</option>
</select>

This doesn't seem to be a very efficient way of passing $color to the select box, but it works. Is there some other way?

TIA...


 
Old August 10th, 2005, 02:16 PM
Registered User
 
Join Date: Aug 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you are not using a database, try the following.
Make sure your form is using the POST method , if not change $_POST to $_GET.

    $color_arr = array('Red'=>'RED',
                                         'Yellow'=>'YEL',
                                         'Green'=>'GRN',
                                         'Blue'=>'BLU',
                                         'White'=>'WHT',
                                         'Black'=>'BLK',
                                         'Brown'=>'BRN');
?>

<select name="color">

<?PHP

    foreach($color_arr as $k => $v)
                        ( $_POST['color'] == $v ) ? print( '<option value='.$v.' selected>'.$k.'</option>' ) : print( '<option value='.$v.'>'.$k.'</option>' );

?>

</select>

<input type="submit" value="submit">






Similar Threads
Thread Thread Starter Forum Replies Last Post
Setting default value in drop down. rupen Classic ASP Basics 4 July 3rd, 2014 02:00 AM
setting default values prithvi28 XSLT 1 October 3rd, 2006 01:38 PM
How to select text by default in a text box? clickajin Java GUI 0 August 3rd, 2006 03:10 AM
drop down select box shoakat Classic ASP Databases 1 February 10th, 2005 04:42 AM
select a default value in a drop-down list crmpicco HTML Code Clinic 2 January 20th, 2005 08:23 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.