Jump to content

Drop down menu needs a "choose" (default item).


edfou
Go to solution Solved by edfou,

Recommended Posts

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.php

Below 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>

Link to comment
Share on other sites

  • Solution

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.