I'm trying to retain the values of the month and year after the form been sent.. Anyone know how i should do it?

User gets to select 02 for month and 2001 for year..
After it is been sent the values go back to - , what i want here is to retain the values of both month and year, 02 and 2001 that is.

$month = array (1=>"-","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
$select .= "\t<option val=\"".$key."\"";
if ($key == 1){
$select .= " selected>".$val."\n";
}//end if
else{
$select .= ">".$val."\n";
}// end else
}// end foreach
$select .= "</select>";
echo $select;

// Displaying the Year in the DateOfOccurence drop down box
$year = date("Y"); //get the current year from the system
echo "<select name='year'>\n"; 
echo '<option value="" selected">-</option>'; 
for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
echo " <option value=\"$n\">$n</option>"; 
} // end for
echo "</select>\n";  
}// end function

    You mean when this <select> list is redisplayed? It's a matter of changing the default selected option. You're half-way there with the months; just test $key against whatevever month was supplied instead of just 1 (but continue to use 1 if it hasn't been supplied). Similarly for the year.

      You mean when this <select> list is redisplayed?

      Yes it is.. I wish to display the values selected after the form been sent

      just test $key against whatevever month was supplied instead of just 1 (but continue to use 1 if it hasn't been supplied)

      You means this
      $key == 1
      But the 1 is for the array.. So what should i do?

        Try something lke this.. you may wish to use loops etc but this will give you a better idea of wats being done.

        
        <?php
          $month = $_POST[month];
          $year = $_POST[year];
        ?>
        
        <select name="month">
          <option <?php if($month == "1"){ echo " selected"; } ?> value="1">Jan</option>
          <option <?php if($month == "2"){ echo " selected"; } ?> value="2">Feb</option>
          <option <?php if($month == "3"){ echo " selected"; } ?> value="3">Mar</option>
          <option <?php if($month == "4"){ echo " selected"; } ?> value="4">Apr</option>
          <option <?php if($month == "5"){ echo " selected"; } ?> value="5">May</option>
          <option <?php if($month == "6"){ echo " selected"; } ?> value="6">Jun</option>
          <option <?php if($month == "7"){ echo " selected"; } ?> value="7">Jul</option>
          <option <?php if($month == "8"){ echo " selected"; } ?> value="8">Aug</option>
          <option <?php if($month == "9"){ echo " selected"; } ?> value="9">Sep</option>
          <option <?php if($month == "10"){ echo " selected"; } ?> value="10">Oct</option>
          <option <?php if($month == "11"){ echo " selected"; } ?> value="11">Nov</option>
          <option <?php if($month == "12"){ echo " selected"; } ?> value="1">Dec</option>
        </select>
        
        <select name="year">
          <option <?php if($year== "2000"){ echo " selected"; } ?> value="2000">2000</option>
          <option <?php if($year== "2001"){ echo " selected"; } ?> value="2001">2001</option>
          <option <?php if($year== "2002"){ echo " selected"; } ?> value="2002">2002</option>
          <option <?php if($year== "2003"){ echo " selected"; } ?> value="2003">2003</option>
          <option <?php if($year== "2004"){ echo " selected"; } ?> value="2004">2004</option>
          <option <?php if($year== "2005"){ echo " selected"; } ?> value="2005">2005</option>
        </select>
        
        

          Or you could simply set the variables in the forms to what you recieved in your POST Arguments, in your case of drop down boxes that would be setting the selected attribute in the right place,

          $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : '';	
          if ($key == $month){
          $select .= " selected>".$val."\n";
          }// end if
          

          So this is what i did to retain the values of the month
          but why is it after i sent the form the value minus 1.

          that is to say.. i sent 12 the value displayed is 11

          $month = array (1=>"-","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
          $select = "<select name=\"month1\">\n";
          foreach ($month as $key => $val) {
          $select .= "\t<option val=\"".$key."\"";
          $month = isset($_REQUEST['month']) ? $_REQUEST['month'] : '';	
          if ($key == $month){
          $select .= " selected>".$val."\n";
          }// end if
          else{
          $select .= ">".$val."\n";
          }// end else
          }// end foreach
          $select .= "</select>";
          echo $select;
          

          As for my year, how should it be done?

          // Displaying the Year in the DateOfOccurence drop down box
          $year = date("Y"); //get the current year from the system
          echo "<select name='year'>\n"; 
          echo '<option value="" selected">-</option>'; 
          for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
          echo " <option value=\"$n\">$n</option>"; 
          } // end for
          echo "</select>\n";  
          }// end function

            chrislive .. How should work with a loop that would allow me to use your method?

            <select name="year"> 
              <option <?php if($year== "2000"){ echo " selected"; } ?> value="2000">2000</option> 
              <option <?php if($year== "2001"){ echo " selected"; } ?> value="2001">2001</option> 
              <option <?php if($year== "2002"){ echo " selected"; } ?> value="2002">2002</option> 
              <option <?php if($year== "2003"){ echo " selected"; } ?> value="2003">2003</option> 
              <option <?php if($year== "2004"){ echo " selected"; } ?> value="2004">2004</option> 
              <option <?php if($year== "2005"){ echo " selected"; } ?> value="2005">2005</option> 
            </select> 
            

            As my year is captured from the system date

            Could something like this work?

            
            <option <?php if($year== "($_GET['year']"){ echo " selected"; } ?> value="$_GET['year']">$year</option> 
            
            

              use this as a base.. then just run it through your loop

              
              <?php
                $c_year = date("Y");
              ?>
              
              <select name="year"> 
                <option <?php if($year == $c_year-10){ echo " selected"; } ?> value="<?php echo $c_year-10 ?>"><?php echo $c_year-10 ?></option>
                <option <?php if($year == $c_year-9){ echo " selected"; } ?> value="<?php echo $c_year-9?>"><?php echo $c_year-9?></option> 
              </select>
              

                because i already using $year in my example to get the POST data: $year = $_POST[year];
                so $c_year is the current year. and the -10 is the amount of years prior you want

                  so is this the way i should do it

                  
                  
                  $year = date("Y"); //get the year from $today 
                  echo "<select name='year1'>\n"; 
                  echo '<option value="" selected">-</option>'; 
                  echo <option if($year == $year-10){ echo " selected"; } value=" echo $year-10"> echo $year-10</option>;
                  echo <option if($year == $year-9){ echo " selected"; }  value=" echo $year-9"> echo $year-9</option>;
                  echo "</select>\n"; 
                  
                  

                  or

                  // Displaying the Year in the DateOfOccurence drop down box
                  $year = date("Y"); //get the current year from the system
                  echo "<select name='year'>\n"; 
                  echo '<option value="" selected">-</option>'; 
                  for ($n=$year;$year-10<=$n;$n--) { // gets the current year and do a subtraction of 10 to display the array of descending 10 years 
                  echo " <option value=\"$n\">$n</option>"; 
                  } // end for
                  echo "</select>\n";
                  
                  
                  <?php 
                    $c_year = date("Y"); 
                  ?> 
                  
                  <select name="year"> 
                    <option <?php if($year == $c_year-10){ echo " selected"; } ?> value="<?php echo $c_year-10 ?>"><?php echo $c_year-10 ?></option> 
                    <option <?php if($year == $c_year-9){ echo " selected"; } ?> value="<?php echo $c_year-9?>"><?php echo $c_year-9?></option> 
                  </select> 
                  

                    must user $year1 not $year cause your $year declaration will overide your POST data

                      Write a Reply...