1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP on <select> onchange event

Discussion in 'PHP' started by lnr1, Sep 28, 2007.

  1. #1
    Hi there.... I have a problem.

    I have a form with two drop down menus.
    One has the days of a month and the other has the name of the month

    When someone changes the month I want the days to change so that there is only the amount of days for the chosen month

    This is what I have so far
    
    <label for="event_date_day">Date:</label>
    <select name="event_date_day" id="event_date_day">
    <?php							
         for($i=1;$i<=$daysInMonth;$i++) {
    ?>
    
    <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
    							
    <?php
    							
          }
    								
    ?>
    </select>
    <select name="event_date_month" id="event_date_month">
    [INDENT]<option>January</option>
    <option>February</option>
    <option>March</option>
    <option>April</option>
    <option>May</option>
    <option>June</option>
    <option>July</option>
    <option>August</option>
    <option>September</option>
    <option>October</option>
    <option>November</option>
    <option>Decemeber</option>[/INDENT]
    </select><br />
    
    Code (markup):
     
    lnr1, Sep 28, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    There's no "onchange" event in PHP. If you want this to happen without submitting the form you have to ask in the JavaScript section.

    So do you want to do this with PHP or JavaScript?
     
    nico_swd, Sep 28, 2007 IP
  3. lnr1

    lnr1 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There is a onChange event of the <select> form though.
    I thought there might be a way in PHP but I will try it through Javascript.

    Thanks
     
    lnr1, Sep 28, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    PHP does nothing after the output has been sent. It is a "pre-processor".

    Simplest method using inline behaviour:
    <script type="text/javascript">
    
    var days_in_month = Array(31,29,31,30,31,31,30,31,30,31); // deal with leap years if you want :D
    
    function byId(id){
      return document.getElementById ?  document.getElementById(id) : document.all[id];
    }
    function populateDays(month) {
      // validate month number here
      var days = days_in_month[month];
      for (i = byId('days').options.length - 1; i>=28; i--) {
        byId('days').remove(i);
      }
      for (i = 28; i <= days; i++) {
        var option = document.createElement('option');
        option.text = option.value = i;
        try      { byId('days').add(option, null); } // non-IE
        catch(e) { byId('days').add(option); } // IE
      }
    }
    </script>
    
    <select onchange="populateDays(this.selectedIndex-1)">
    <option>---</option>
    <option>January</option>
    <option>February</option>
    </select>
    <select id="days">
    <option>---</option>
    <option>1</option>
    <option>2</option>
    ... (fill this list to provide non JS fallback)
    </select>
    
    Code (markup):
     
    krt, Sep 28, 2007 IP
  5. msaqibansari

    msaqibansari Peon

    Messages:
    84
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nice code KRT....
     
    msaqibansari, Sep 28, 2007 IP