This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Webmaster forum > June 2006 > setting value for select in form





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author setting value for select in form
Patrick

2006-06-03, 7:13 pm

Hello All,

Is there a way to preset a default value in a select in a form?

If you have something like below you can do it with value="";

Ship name:<br> <input class="inform" type="text" size="30"
name="shipname" value="Suncoaster">

If you have something like below, value="" doesn't seem to work. What if
I wanted it to default to the current month? Or read a month from a file
using a PHP echo?

<select class="inform" name="logmonth">
<option value="1">Jan
<option value="2">Feb
<option value="3">Mar
<option value="4">Apr
<option value="5">May
<option value="6">Jun
<option value="7">Jul
<option value="8">Aug
<option value="9">Sep
<option value="10">Oct
<option value="11">Nov
<option value="12">Dec

Hope you can help.

Thanks,
Patrick

--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - college of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld

GreyWyvern

2006-06-03, 7:13 pm

And lo, Patrick didst speak in alt.www.webmaster:

> Hello All,
>
> Is there a way to preset a default value in a select in a form?


<select class="inform" name="logmonth">
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
<option value="4">Apr</option>
<option value="5" selected="selected">May</option>
<option value="6">Jun</option>
<option value="7">Jul</option>
<option value="8">Aug</option>
<option value="9">Sep</option>
<option value="10">Oct</option>
<option value="11">Nov</option>
<option value="12">Dec</option>
</select>

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
Patrick

2006-06-03, 7:13 pm

GreyWyvern wrote:

> And lo, Patrick didst speak in alt.www.webmaster:
>
>
>
> <select class="inform" name="logmonth">
> <option value="1">Jan</option>
> <option value="2">Feb</option>
> <option value="3">Mar</option>
> <option value="4">Apr</option>
> <option value="5" selected="selected">May</option>
> <option value="6">Jun</option>
> <option value="7">Jul</option>
> <option value="8">Aug</option>
> <option value="9">Sep</option>
> <option value="10">Oct</option>
> <option value="11">Nov</option>
> <option value="12">Dec</option>
> </select>
>
> Grey
>

Grey,

Wonderful, thank you. I see you added the closing </option> tag. I've
seen it both ways, closed and unclosed. Is closing the tag the preferred
way. Also now that I have this response, how would I go about doing this
dynamically so that I can read a month from a file and have PHP echo it
and the form show the correct month in the box when accessed? For other
parts of the form I have been doing a

<input class="inform" type="text" size="30" name="shipname" value="<?php
echo $larray[$i++]; ?>">

to display the shipname reading from a flat text file. Basically what I
am trying to do here is take a form output that has already been
compiled into a text file then allowing our scientist to re-open it at a
later date and modify the inputs and saving/submitting it to a revision2.


Thanks,
Patrick

--
Patrick A. Smith Assistant System Administrator
Ocean Circulation Group – USF - college of Marine Science
http://ocgweb.marine.usf.edu Phone: 727 553-3334

The trouble with doing something right the first time is that nobody
appreciates how difficult it was. - La Rochefoucauld

GreyWyvern

2006-06-03, 7:13 pm

And lo, Patrick didst speak in alt.www.webmaster:

> I see you added the closing </option> tag. I've seen it both ways,
> closed and unclosed. Is closing the tag the preferred way.


It is optional in HTML 4.0, required in XHTML 1.0. In any case, I don't
like shortcuts; I always close them, even when I use an HTML DOCTYPE.

> Also now that I have this response, how would I go about doing this
> dynamically so that I can read a month from a file and have PHP echo it
> and the form show the correct month in the box when accessed?


....
<option value="4"<?php if (date("n") == 4) echo " selected=\"selected\"";
?>>Apr</option>
<option value="5"<?php if (date("n") == 5) echo " selected=\"selected\"";
?>>May</option>
<option value="6"<?php if (date("n") == 6) echo " selected=\"selected\"";
?>>Jun</option>
....

You may want to do the whole thing with an array to save bytes:

<?php
$months = array(
1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr",
5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug",
9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"
);
$thismonth = (int)date("n");
?>

<select class="inform" name="logmonth"><?php
foreach ($months as $key => $value) {
echo "\n <option value=\"$key\"",
($key == $thismonth) ? " selected=\"selected\"" : "",
">$value</option>";
} ?>
</select>


Just change $thismonth to whatever you read from your file.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured
spider and site-search engine
pasusf

2006-06-03, 7:13 pm

GreyWyvern wrote:
> And lo, Patrick didst speak in alt.www.webmaster:
>
>
> It is optional in HTML 4.0, required in XHTML 1.0. In any case, I don't
> like shortcuts; I always close them, even when I use an HTML DOCTYPE.
>
>
> ...
> <option value="4"<?php if (date("n") == 4) echo " selected=\"selected\"";
> ?>>Apr</option>
> <option value="5"<?php if (date("n") == 5) echo " selected=\"selected\"";
> ?>>May</option>
> <option value="6"<?php if (date("n") == 6) echo " selected=\"selected\"";
> ?>>Jun</option>
> ...
>
> You may want to do the whole thing with an array to save bytes:
>
> <?php
> $months = array(
> 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr",
> 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug",
> 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"
> );
> $thismonth = (int)date("n");
> ?>
>
> <select class="inform" name="logmonth"><?php
> foreach ($months as $key => $value) {
> echo "\n <option value=\"$key\"",
> ($key == $thismonth) ? " selected=\"selected\"" : "",
> ">$value</option>";
> } ?>
> </select>
>
>
> Just change $thismonth to whatever you read from your file.
>
> Grey


Thanks again Grey, I will give that a whirl. Sorry to be replying from
Google Groups but the USF news server went down. Not an uncommon
occurence.

Patrick

David Dorward

2006-06-03, 7:13 pm

Patrick wrote:
> Is there a way to preset a default value in a select in a form?


http://www.w3.org/TR/html4/interact/forms.html#h-17.6.1

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Charles Sweeney

2006-06-03, 7:13 pm

Patrick wrote

> Hello All,


Hello Patrick!

--
Charles Sweeney
http://CharlesSweeney.com
David Dorward

2006-06-03, 7:13 pm

GreyWyvern wrote:

> <option value="5" selected="selected">May</option>


Avoid if you are working with HTML.
http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.4

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
GreyWyvern

2006-06-03, 7:13 pm

And lo, David Dorward didst speak in alt.www.webmaster:

> GreyWyvern wrote:
>
>
> Avoid if you are working with HTML.
> http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.4


.... said the specification written in 1999. There is no longer a
significant market share of people using browsers that do not support the
proper HTML expanded attributes.

Grey

--
The technical axiom that nothing is impossible sinisterly implies the
pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search - PHP/MySQL site
search engine
David Dorward

2006-06-03, 7:14 pm

GreyWyvern wrote:
[color=darkred]
> ... said the specification written in 1999. There is no longer a
> significant market share of people using browsers that do not support the
> proper HTML expanded attributes.


So? Type less, use slightly less bandwidth and don't cause problems for a
small number of people.

Its win-win, even if its a small win.

--
David Dorward <http://blog.dorward.me.uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
pasusf

2006-06-03, 7:15 pm

Charles Sweeney wrote:
> Patrick wrote
>
>
> Hello Patrick!
>

Hello Charles!

You've got to love this guy. It's like "Oliver Twist". "Consider
yourself at home. Consider yourself one of the family."

Patrick

Charles Sweeney

2006-06-03, 7:15 pm

pasusf wrote

> Charles Sweeney wrote:
> Hello Charles!
>
> You've got to love this guy. It's like "Oliver Twist". "Consider
> yourself at home. Consider yourself one of the family."


"We've taken to you so strong. It's clear...we're...going to get along"

--
Charles Sweeney
http://CharlesSweeney.com
Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews