edfou Posted October 26, 2013 Share Posted October 26, 2013 Hi guys, I would really appreciate some help with this code. How it works now is that it creates a drop down menu for date starting today and for the last 14 days. There is also a time drop down menu. Both correctly retain the selected values if the page is refreshed.Now in addition I need the following to work:1. have the word "choose" as the displayed default menu item instead of the initial date and initial time values that show now.2. secondly if a value is NOT chosen from either menu (and after hitting Submit button) I want the error to display similar to if other required fields are not filled in. It seems that because the default menu value IS already a day value, the script thinks that there is already a POST from these menus and will therefore not display the error which checks for each menu POST.<? echo $err_day; ?><? echo $err_time; ?>Here's my script online:http://www.cmfsc.ca/forms/lostandfound-WORKING.phpBelow is the relevant code that needs to be tweaked somehow. Thanks!!! Ed <tr> <td>Date lost/found *</td> <td><select name="day"> <? date_default_timezone_set("America/Vancouver"); $startDay = mktime(); $endDay = mktime() - (14 * 24 * 3600); for ($i = $startDay; $i >= $endDay; $i = $i - 86400) { $thisDate = date('l F jS Y', $i); // nothing selected by default $selected = ''; // if the form is submitted. // Check that the posted value is the same as this value // if is the same set it to selected. if(isset($_POST['day']) && $_POST['day'] == $thisDate) $selected = ' selected'; // added the selected attribute here \ / print "<option value=\"$thisDate\"$selected>$thisDate</option>\n"; } ?> </select> </td> </tr> <tr> <td>Time lost/found *</td> <td><select name="time"> <? $startTime = mktime(9, 00, 0, 0, 0, 0); $endTime = mktime(20, 00, 0, 0, 0, 0); for ($i = $startTime; $i <= $endTime; $i = $i + 1800) { $thisTime = date('g:i a', $i); // nothing selected by default $selected = ''; // if the form is submitted. // Check that the posted value is the same as this value // if is the same set it to selected. if(isset($_POST['time']) && $_POST['time'] == $thisTime) $selected = ' selected'; // added the selected attribute here print "<option value=\"$thisTime\"$selected>$thisTime</option>\n"; } ?> </select> </td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/283295-drop-down-menu-needs-a-choose-default-item/ Share on other sites More sharing options...
Barand Posted October 26, 2013 Share Posted October 26, 2013 Make the first option in each select <option value=''> Choose</option> If no other is "selected" then the first is shown by default Quote Link to comment https://forums.phpfreaks.com/topic/283295-drop-down-menu-needs-a-choose-default-item/#findComment-1455505 Share on other sites More sharing options...
Solution edfou Posted October 26, 2013 Author Solution Share Posted October 26, 2013 Hi Barand, thanks a lot for the tip! I used that to touch up the code and it now works great. Code changes in bold below if anyone else can benefit from this too. Cheers Ed <td>Date lost/found *</td> <td><select name="day"> <? print "<option value=\"\">choose</option>\n"; date_default_timezone_set("America/Vancouver"); $startDay = mktime(); $endDay = mktime() - (14 * 24 * 3600); for ($i = $startDay; $i >= $endDay; $i = $i - 86400){ $thisDate = date('l F jS Y', $i); // nothing selected by default $selected = ''; // if the form is submitted. // Check that the posted value is the same as this value // if is the same set it to selected. if(isset($_POST['day']) && $_POST['day'] == $thisDate) $selected = ' selected'; // added the selected attribute here \/ print "<option value=\"$thisDate\"$selected>$thisDate</option>\n"; } ?> </select> </td> </tr> <tr> <td></td><td><? echo $err_day; ?></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/283295-drop-down-menu-needs-a-choose-default-item/#findComment-1455557 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.