Setting Default Value of Select - Option Tag in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Setting Default Value of Select - Option Tag in PHP

Post by devarishi »

Let's suppose Support Group has the value (in the database) "Help Desk" (it could be any other value also) and on this web page which is meant for chaning the value (if required so), the original value should be selected by default:

Code: Select all

Support Group   <select name="supportGroup" value= <?php echo '"' . $rs->Fields['supportGroup']->Value . '"'; ?> >
    <option value="Backup"> Backup </option>
                                        <option value="DBA Support"> DBA Support  </option>
                                        <option value="Help Desk"> Help Desk </option>
</select>   
However, it displays the first option value, i.e. "Backup".

Any ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Setting Default Value of Select - Option Tag in PHP

Post by requinix »

Basic HTML knowledge.

Code: Select all

<option value="Help Desk" selected="selected"> Help Desk </option>
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Re: Setting Default Value of Select - Option Tag in PHP

Post by devarishi »

tasairis wrote:Basic HTML knowledge.

Code: Select all

<option value="Help Desk" selected="selected"> Help Desk </option>


Hi,

That is okay. But the value of the option element is coming from a database (table).

Code: Select all

<?php echo '"' . $rs->Fields['supportGroup']->Value . '"'; ?>
Whatever value is there, it has to be displayed in the Drop-Down-Menu by default. The purpose is to enable the user change its value if he/she wants to. Otherwise leave it as it is. And in the later case, the original value must be displayed so that when the Select Element is referred in another web page we get a value depending upon whether it was changed or not.

Note: There are several other text elements on the same page and all of them are editable so that one can change their values if required. Otherwise, their original values are passed to the next web page for processing (updating).
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Setting Default Value of Select - Option Tag in PHP

Post by requinix »

Okay. So only output the selected="selected" if it's supposed to be.

Hint: if.
Post Reply