By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,721 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,721 IT Pros & Developers. It's quick & easy.

Can a default value be specified for a drop-down list?

P: n/a
I populate a drop-down list like this:

echo "<select>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";

Is it possible to have the selection in the drop-down list default to a
particular value? For example, let's say the complete list of values looks
like this:

65.162.31.155
210.213.149.6
68.190.20.83
130.202.234.254
68.122.35.157

When the page is loaded, I want 68.190.20.83 to appear as the selected value
in the drop-down list. Can this be done? Any suggestions or examples
welcome!

Thanks!
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
deko <ww*******************************@nospam.com> wrote:
I populate a drop-down list like this:

echo "<select>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";

Is it possible to have the selection in the drop-down list default to a
particular value? For example, let's say the complete list of values looks
like this:

65.162.31.155
210.213.149.6
68.190.20.83
130.202.234.254
68.122.35.157

When the page is loaded, I want 68.190.20.83 to appear as the selected value
in the drop-down list. Can this be done? Any suggestions or examples
welcome!

Thanks!


This is an HTML-issue, not PHP. You are looking for <option selected>
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #2

P: n/a
> This is an HTML-issue, not PHP. You are looking for <option selected>

10-4. I will research <option selected> and see if I can find a solution.
Jul 17 '05 #3

P: n/a
"deko" <ww*******************************@nospam.com> wrote in
news:Sn***************@newssvr13.news.prodigy.com:
I populate a drop-down list like this:

echo "<select>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";

Is it possible to have the selection in the drop-down list default to
a particular value? For example, let's say the complete list of
values looks like this:

65.162.31.155
210.213.149.6
68.190.20.83
130.202.234.254
68.122.35.157

When the page is loaded, I want 68.190.20.83 to appear as the selected
value in the drop-down list. Can this be done? Any suggestions or
examples welcome!

Thanks!


There is probably a better way to do this since this was just a quick and
dirty method to make it work for me.....

HTML requires the particular menu choice to be listed as 'selected'. So
you can make an array of choices (listed by name instead of number) that
match the ones in the HTML, default them to nothing. and set the proper
one to 'selected'. In the HTML, add php code to each choice. The ones
that are blank will be ignored. The one with 'selected' will be the
choice.

the starting php
$select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>"");
$select['porche'] = 'selected';

The normal HTML unselected option line looks like this
<option value="none">Porche</option>

The line that is selected via HTML is
<option value="none" selected>Porche</option>

The line using a php array is
<option value="none" <? print $select['porche']; ?> >None</option>

If porche was set as 'selected' when setting up the array, it will match
example #2. If not, it will match #1. You can expand on this by using
choices in a database instead of creating the array manually.

If there IS a better way I will also be interested ;o)
Jul 17 '05 #4

P: n/a
Theo <in*****@noemail.com> wrote:
"deko" <ww*******************************@nospam.com> wrote in
news:Sn***************@newssvr13.news.prodigy.com:
I populate a drop-down list like this:

echo "<select>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";

Is it possible to have the selection in the drop-down list default to
a particular value? For example, let's say the complete list of
values looks like this:

65.162.31.155
210.213.149.6
68.190.20.83
130.202.234.254
68.122.35.157

When the page is loaded, I want 68.190.20.83 to appear as the selected
value in the drop-down list. Can this be done? Any suggestions or
examples welcome!

Thanks!


There is probably a better way to do this since this was just a quick and
dirty method to make it work for me.....

HTML requires the particular menu choice to be listed as 'selected'. So
you can make an array of choices (listed by name instead of number) that
match the ones in the HTML, default them to nothing. and set the proper
one to 'selected'. In the HTML, add php code to each choice. The ones
that are blank will be ignored. The one with 'selected' will be the
choice.

the starting php
$select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>"");
$select['porche'] = 'selected';

The normal HTML unselected option line looks like this
<option value="none">Porche</option>

The line that is selected via HTML is
<option value="none" selected>Porche</option>

The line using a php array is
<option value="none" <? print $select['porche']; ?> >None</option>

If porche was set as 'selected' when setting up the array, it will match
example #2. If not, it will match #1. You can expand on this by using
choices in a database instead of creating the array manually.

If there IS a better way I will also be interested ;o)


-----BEGIN PHP CODE BLOCK-----
$options = array(
'option1',
'option2',
'option3',
'option4'
);
$selected = 1; // option2

for ($i=0, $len=sizeof($options); $i<$len; $i++)
echo '<option value="'.$i.
(($i==$selected) // select this option?
? ' selected>'
: '>').
$options[$i].'</option>';
------END PHP CODE BLOCK------

--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #5

P: n/a
> > There is probably a better way to do this since this was just a quick
and
dirty method to make it work for me.....

HTML requires the particular menu choice to be listed as 'selected'. So
you can make an array of choices (listed by name instead of number) that
match the ones in the HTML, default them to nothing. and set the proper
one to 'selected'. In the HTML, add php code to each choice. The ones
that are blank will be ignored. The one with 'selected' will be the
choice.

the starting php
$select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>""); $select['porche'] = 'selected';

The normal HTML unselected option line looks like this
<option value="none">Porche</option>

The line that is selected via HTML is
<option value="none" selected>Porche</option>

The line using a php array is
<option value="none" <? print $select['porche']; ?> >None</option>

If porche was set as 'selected' when setting up the array, it will match
example #2. If not, it will match #1. You can expand on this by using
choices in a database instead of creating the array manually.

If there IS a better way I will also be interested ;o)


-----BEGIN PHP CODE BLOCK-----
$options = array(
'option1',
'option2',
'option3',
'option4'
);
$selected = 1; // option2

for ($i=0, $len=sizeof($options); $i<$len; $i++)
echo '<option value="'.$i.
(($i==$selected) // select this option?
? ' selected>'
: '>').
$options[$i].'</option>';
------END PHP CODE BLOCK------


hmmm... that seems like a lot of code.

The below code seems to work - that is, the drop-down list shows $select_ip
as the selected choice when the page loads, and all the other IPs are still
available for selection in the list.

echo "<select>";
echo "<option selected value=".$select_ip.">".$select_ip."</option>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";
Jul 17 '05 #6

P: n/a
deko <ww*******************************@nospam.com> wrote:
There is probably a better way to do this since this was just a quick and dirty method to make it work for me.....

HTML requires the particular menu choice to be listed as 'selected'. So
you can make an array of choices (listed by name instead of number) that
match the ones in the HTML, default them to nothing. and set the proper
one to 'selected'. In the HTML, add php code to each choice. The ones
that are blank will be ignored. The one with 'selected' will be the
choice.

the starting php
$select = array("none"=>"", "porche"=>"", "mustang"=>"", "corvette"=>""); $select['porche'] = 'selected';

The normal HTML unselected option line looks like this
<option value="none">Porche</option>

The line that is selected via HTML is
<option value="none" selected>Porche</option>

The line using a php array is
<option value="none" <? print $select['porche']; ?> >None</option>

If porche was set as 'selected' when setting up the array, it will match
example #2. If not, it will match #1. You can expand on this by using
choices in a database instead of creating the array manually.

If there IS a better way I will also be interested ;o)


-----BEGIN PHP CODE BLOCK-----
$options = array(
'option1',
'option2',
'option3',
'option4'
);
$selected = 1; // option2

for ($i=0, $len=sizeof($options); $i<$len; $i++)
echo '<option value="'.$i.
(($i==$selected) // select this option?
? ' selected>'
: '>').
$options[$i].'</option>';
------END PHP CODE BLOCK------


hmmm... that seems like a lot of code.

The below code seems to work - that is, the drop-down list shows $select_ip
as the selected choice when the page loads, and all the other IPs are still
available for selection in the list.

echo "<select>";
echo "<option selected value=".$select_ip.">".$select_ip."</option>";
foreach ( $ip_list as $var )
{
echo "<option>";
echo $var;
echo "</option>";
}
echo "</select>";


You are not required to put the selected value in the first place.
--
Simon Stienen <http://dangerouscat.net> <http://slashlife.de>
»What you do in this world is a matter of no consequence,
The question is, what can you make people believe that you have done.«
-- Sherlock Holmes in "A Study in Scarlet" by Sir Arthur Conan Doyle
Jul 17 '05 #7

P: n/a
$list=array(...); // any hash or indexed list
$select=2; // key of selected item in $list

foreach($list as $id => $item)
echo "<option value=\"$id\"".
(($id==$select)?(" selected"):("")).
">$item</option>";
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.