tags:

views:

311

answers:

1

I'm getting a strange behaviour from form_dropdown - basically, when I reload the page after validation, the values are screwed up.

this bit generates 3 drop downs with days, months and years:

$days = array(0 => 'Day...');
        for ($i = 1; $i <= 31; $i++)
        {
            $days[] = $i;
        }
        $months = array(0 => 'Month...', );
        for ($i = 1; $i <= 12; $i++)
        {
            $months[] = $i;
        }
        $years = array(0 => 'Year...');
        for ($i = 2010; $i <= 2012; $i++)
        {
            $years[$i] = $i; 
            echo "<pre>"; print_r($years); echo "</pre>";//remove this
        }

        $selected_day = (isset($selected_day)) ? $selected_day : 0;
        $selected_month = (isset($selected_month)) ? $selected_month : 0;
        $selected_year = (isset($selected_year)) ? $selected_year : 0;
        echo "<p>";
            echo form_label('Select date:', 'day', array('class' => 'left'));
            echo form_dropdown('day', $days, $selected_day, 'class="combosmall"'); 
            echo form_dropdown('month', $months, $selected_month, 'class="combosmall"'); 
            echo form_dropdown('year', $years, $selected_year, 'class="combosmall"'); 
        echo "</p>";

...and generates this:

    <p><label for="day" class="left">Select date:</label><select name="day" class="combosmall"> 
<option value="0" selected="selected">Day...</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
<option value="6">6</option> 
<option value="7">7</option> 
<option value="8">8</option> 
<option value="9">9</option> 
<option value="10">10</option> 
<option value="11">11</option> 
<option value="12">12</option> 
<option value="13">13</option> 
<option value="14">14</option> 
<option value="15">15</option> 
<option value="16">16</option> 
<option value="17">17</option> 
<option value="18">18</option> 
<option value="19">19</option> 
<option value="20">20</option> 
<option value="21">21</option> 
<option value="22">22</option> 
<option value="23">23</option> 
<option value="24">24</option> 
<option value="25">25</option> 
<option value="26">26</option> 
<option value="27">27</option> 
<option value="28">28</option> 
<option value="29">29</option> 
<option value="30">30</option> 
<option value="31">31</option> 
</select><select name="month" class="combosmall"> 
<option value="0" selected="selected">Month...</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
<option value="4">4</option> 
<option value="5">5</option> 
<option value="6">6</option> 
<option value="7">7</option> 
<option value="8">8</option> 
<option value="9">9</option> 
<option value="10">10</option> 
<option value="11">11</option> 
<option value="12">12</option> 
</select><select name="year" class="combosmall"> 
<option value="0" selected="selected">Year...</option> 
<option value="2010">2010</option> 
<option value="2011">2011</option> 
<option value="2012">2012</option> 
</select></p>

however, when the form is reloaded after validation, the same code above generates this:

<!-- days and months... -->
<select name="year" class="combosmall"> 
<option value="0" selected="selected">Year...</option> 
<option value="1">2010</option> 
<option value="2">2011</option> 
<option value="3">2012</option> 
</select>

So basically the value start from 1 instead of 2010. The same happens to days and months but obviously it doesn't make any difference in this particular case as the values would start from 1 anyway.

How can I fix this - and why does it happen?

edit: validation rules are:

$this->load->library('form_validation');
//...rules for other fields..
$this->form_validation->set_rules('day', 'day', 'required|xss_clean');
$this->form_validation->set_rules('month', 'month', 'required|xss_clean');
$this->form_validation->set_rules('year', 'year', 'required|xss_clean');        
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');
//define other errors
if($this->input->post('day') == 0 || $this->input->post('month') == 0 || $this->input->post('year') == 0) 
    {
        $data['error'] = "Please check the date of your event.";
    }         
A: 

Not sure why this is happening. Everything looks ok (although you probably don't need 'required' and your user-defined check!). Perhaps try using a different variable as your counter for days, weeks and years. ie.

$days = array(0 => 'Day...');
    for ($d = 1; $d <= 31; $d++)
    {
        $days[] = $d;
    }
    $months = array(0 => 'Month...', );
    for ($m = 1; $m <= 12; $m++)
    {
        $months[] = $m;
    }
    $years = array(0 => 'Year...');
    for ($y = 2010; $y <= 2012; $y++)
    {
        $years[$y] = $y; 
    }

I'm not sure if this will work or why it should any better than what you have, but might be worth a try?!?

If it doesn't work, put each array through var_dump() before you send it to the form_dropdown method to see if it's something going wrong with generating the array or whether something is going funny in the helper function. Of course, it wouldn't be much extra work to code it in pure php rather than using the helper! ;-)

musoNic80
I've changed the variables and now it works. However, I don't think that has solved it, as for a while it kept producing errors even if I had modified and saved the file. Probably the file was cached or didn't get updated for some reason, and maybe was still 'loading' a previous version in which I had a typo: the last statement was $years[] = $y Very WEIRD anyway! Will keep an eye on it
Patrick
Yes, make sure you're not reloading the page from a cached version or old version. Fingers crossed it's solved now. Don't forget to accept my answer if it turns out to have helped!
musoNic80