Setting a default value to a form

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
timbojones
Forum Newbie
Posts: 2
Joined: Mon Jun 26, 2006 6:46 am

Setting a default value to a form

Post by timbojones »

I have form that sets a value (choice of a year) from a list of options (range of years) that in turn displays related data in a table (a calendar of months).

My question: how do I set a default value (the current year) so that when the page is first accessed the table is populated with data?

When the page is first accessed table has no data.

Thanks in advance.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

A default vlaue in the database or a default value on the page?

On the page it's just:

Code: Select all

<select>
    <option value="zip">Button</option>
    <option value="foo" selected="selected">Bar</option>
</select>
In the database you define it when you create the table:

Code: Select all

create table foo id int auto_increment primary key, foo varchar(10) default 'bar'
User avatar
Verminox
Forum Contributor
Posts: 101
Joined: Sun May 07, 2006 5:19 am

Post by Verminox »

Expanding on d11wtq's explanation, for drop downs you select them just like he posted above. While for radio buttons or checkboxes, you use: checked="checked" ; like this...

Code: Select all

<input type="checbox" name="foo" value="1" checked="checked" />
<!-- OR -->
<input type="radio" name="bar" value="bar" checked="checked" />
timbojones
Forum Newbie
Posts: 2
Joined: Mon Jun 26, 2006 6:46 am

More details . . .

Post by timbojones »

Thanks for the speedy replies!

I have a table displaying the 12 months of the year. The contenst of each cell of the table are generated by php code. The form allows the selection of the year. When the page first displays, no year is selected and no data displays in the table. I want the current year to display when the page first loads.

No database involved with this solution. I'm using the following code in the form:

<form method="post" action="<? echo $_server['PHP_SELF'] ?>">

This is a single, self-contained page.

The 'selected="selected"' approach does not help. The second "database" solution is over my head.

I'm a bit of a newbie at this. Any help greatly appreciated.

Thanks!
Post Reply