SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Feb 2002
    Location
    Wageningen, the Netherlands
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date Dropdown Menu

    The next code makes a Date Dropdown Menu on a website, what do I have to add to the code to sent a chosen date to a textfile (date.txt) on the servers harddrive ?

    HTML Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <?php
    //echo date_dropdown();
    function date_dropdown($year_limit = 0){
            $html_output = '    <div id="date_select" >'."\n";
            $html_output .= '        <label for="date_day">Select date/label>'."\n";
    
            /*days*/
            $html_output .= '           <select name="date_day" id="day_select">'."\n";
                for ($day = 1; $day <= 31; $day++) {
                    $html_output .= '               <option>' . $day . '</option>'."\n";
                }
            $html_output .= '           </select>'."\n";
    
            /*months*/
            $html_output .= '           <select name="date_month" id="month_select" >'."\n";
            $months = array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
                for ($month = 1; $month <= 12; $month++) {
                    $html_output .= '               <option value="' . $month . '">' . $months[$month] . '</option>'."\n";
                }
            $html_output .= '           </select>'."\n";
    
            /*years*/
            $html_output .= '           <select name="date_year" id="year_select">'."\n";
                for ($year = 2001; $year <= 2012; $year++) {
                    $html_output .= '               <option>' . $year . '</option>'."\n";
                }
            $html_output .= '           </select>'."\n";
    
            $html_output .= '   </div>'."\n";
     return $html_output;
    }
    $text=date_dropdown();
    
    echo $text;
    
    ?>
    </html>

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,996
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by fritsant View Post
    what do I have to add to the code to sent a chosen date to a textfile (date.txt) on the servers harddrive?
    Normally you would do this when the form is submitted.

    E.g.

    PHP Code:
    <form action="myScript.php" method="post">
      <
    label for="date_day">Select date</label>
      <
    select name="date_day" id="day_select">...</select>
      <
    select name="date_month" id="day_month">...</select>
      <
    select name="date_year" id="day_year">...</select>
      <
    input type="submit" value="Submit" />
    </
    form
    This submits to:

    myScript.php
    HTML Code:
    <?php
      $date = $_POST['date_day'] . "-" . $_POST['date_month'] . "-" . $_POST['date_year'];
      $f = fopen("date.txt", "w");
      fwrite($f,$date);
      fclose($f);
    ?>
    However, a text file might not be the best method of storing this information.
    Did you think about using a database instead?

  3. #3
    SitePoint Member
    Join Date
    Feb 2002
    Location
    Wageningen, the Netherlands
    Posts
    15
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your reply, as I want leading zeros I made a small addition to your code.
    HTML Code:
      echo $date = $_POST['date_day'] . "-" . $_POST['date_month'] . "-" . $_POST['date_year'];
      $newDate = date("Y-m-d", strtotime($date));
      $f = fopen("date.txt", "w");
      fwrite($f,$newDate );
      fclose($f);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •