SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Display months and days in dropdowns

    I have a site where I have users put in data for events. In that site I have a dropdown for the month, another for the days of the months, and another for year. The month and year I can set up with a static array of them. However, since each month has a different number of days, I want that month to only show the number of days for that particular month.

    I have tried looking for a solution, but, well, did not find what I believe is a working solution.

    I would like this to be a simple as possible and to use mostly - if not completely - PHP but do realize that some javascript might be necessary.

    Thanks
    E

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    'some javascript' would definately be necessary if you dont want to reload the page - PHP has no ability to manipulate a page once it has been rendered to the browser. an AJAX call to another PHP page would work fine; that page can call the function cal_days_in_month.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion, but I (1) don't want to navigate away from the page and, (2) have to refresh (one of my pet peeves is page refreshing "for no apparent reason"). So, might need some javascript but not sure how or what to do/use.

    E

  4. #4
    SitePoint Addict FizixRichard's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    372
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well the easiest way to do it, I would say, would be to generate your months normally through PHP and then attach some JavaScript that updates the day dropdown with the correct number of days for the selected month.

    So when the month dropdown changes, update the day select to have the correct number of fields. This would be a fairly simple thing to achieve if you know basic JavaScript.


    Totally untested, and written on the fly, but something like this:

    Code:
    <select name="month" onchange="setDay(this.value)"> 
    
    <option value="january">January</option>
    <option value="february">February</option>
    <option value="march">March</option>
    ... and so on
    </select>
    
    <select name="day" id="day">
       <option value="1"> 1</option>
       <option value="2"> 2</option>
       <option value="3"> 3</option>
       ... and so on to 31; probably don't need these at all to be honest ...
    </select>

    Then a javascript function called setDay() which will update the day dropdowns options


    Code:
    function setDay(v) 
    {
       // Latch onto the select we are updating by ID (day)
       var select = document.getElementById("day");
    
    
       // Set end day depending on the month (you could use IF statements to combine months actually)
       switch(v) 
       {
          case "january":
          // Set end day to 31
    	  endday = 31;
          break;
    
          case "febuary":
          // Set end day to 29 (you'll need to account for leap years)
    	  endday = 29;
          break;
    
          case "march":
          // Set end day to 31
    	  endday = 31;
          break;
       }
    
       // Loop from 1 to endday creating select options
       for(i=1;i<=endday;i++)
       {
    	   select.options[i] = new Option(i, i, true, false);
       }
    }
    Something like that anyway.
    FIZIX - Full Service Digital Agency - Engaging websites, apps and games.
    Follow us @FIZIXAgency

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    http://www.w3schools.com/ajax/ajax_aspphp.asp
    A quick example on how to AJAX-call a dynamic page. I'd recommend reading that entire thing from the beginning if you want to learn what it's doing.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Zealot fredep57's Avatar
    Join Date
    Aug 2009
    Location
    Pacific Northwest
    Posts
    137
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FizixRichard, tried your example and, well, it kinda works just does not put them in the listbox.

    StarLion, I read and re-read your link and am still very confused.

    Thanks
    E


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
  •