PDA

View Full Version : How Can I Programmatically Set Defaults for HTML Menus?


mrjonman
09-22-2005, 01:32 PM
Hello all

I'm new to this forum. Please forgive me if the answer to this question is archived here - I couldn't find anything.

I hope I'm calling these items (html menus) by their correct name. I'm talking about the dropdowns created in html with <select></select> tags.

I'm developing a tracking program that connects to a mySQL database for my own use. In it I have a number of html menus used to select beginning and ending dates for different screens. I want the dropdowns to "remember" the last setting I selected before submitting the form.

I can write a value to the database to store default values but I'm not sure how to programmatically add "selected" to the menu's code to cause a certain value to be displayed the next time the page is displayed. Is there another way to set the default value for an html menu?

I hope I'm being clear enough. Any help would be greatly appreciated.

Thanks

John

jonirvine
09-22-2005, 01:52 PM
Hi John,

I'm not sure I'm completely understanding you, but it sounds like you have a date inside a drop down menu and when editing it, it doesn't show the date previously selected.....

If this is the case.....

1) STEP 1 - Extract data from mySQL database

Extract all the data, as you would, normally from the database.

2) SPLIT date value

list($THISyear,$THISmonth,$THISday) = explode('-',$THISrow['date']);

You now have values for the dates year, month and day in the variables above.

3) Show drop downs.

Now you have the values you need to create the drop downs and make sure if the current day is equal to that above it is selected.. and the same for month and year.

So, for day:

echo '<select name="day">'."\n";
$STARTday = 1;
while ($STARTday <= 31) {
echo '<option value="'.$STARTday.'"';
if($STARTday==$THISday) { echo ' selected="selected" ';}
echo '>'.$STARTday.'</option>'."\n";
$STARTday++;
}
echo '</select>'."\n";

For Month:

echo '<select name="month">'."\n";
echo '<option value="01"';
if($THISmonth==01) {echo ' selected="selected" ';}
echo '>January</option>'."\n";
echo '<option value="01"';
if($THISmonth==02) {echo ' selected="selected" ';}
echo '>February</option>'."\n";
//............ etc...............
echo "</select> ";

for year:

$STARTyear = 2000; $ENDyear = date("Y")+10;
echo '<select name="year">'."\n";
while ($STARTyear <= $ENDyear) {
echo '<option value="'.$STARTyear.'"';
if($STARTyear==$THISyear) {echo ' selected="selected"';}
echo '>'.$STARTyear.'</option>'."\n";
$STARTyear++;
}
echo '</select>'."\n";

Hope this makes sense and is what you're looking for.... i rushed it a little.

Jon

mrjonman
09-24-2005, 01:07 PM
Hi Jon

Thanks, it's exactly what I was looking for!

I do have one other problem with my webpage. I have a form with a submit button. I would like to know when the submit button has been clicked (versus refreshing the page)

I would think this has to be a simple php technique that as a complete php newb, I'm just not aware of. :rolleyes:

Any suggestions would be appreciated.

Thank you

John