Welcome Guest, Not a member yet? Register   Sign In
Set select problem after form validation
#1

[eluser]Dave S[/eluser]
I have a form on which I ask people to enter their birthdate by choosing from a select menu. I am having a problem getting the set_select to work so that the year they have chosen is still selected in case the validation fails. The problem is occurring because I have the years loading in a loop like so:

Code:
<select name="BirthYear">
        <option value="">Year</option>
        &lt;?PHP
            $CurrentYear = date("Y");
            $StartYear = $CurrentYear - 100;
            while ($CurrentYear >= $StartYear) {
                echo "<option value=\"$CurrentYear\"".$this->validation->set_select('BirthYear', '$CurrentYear').">$CurrentYear</option>";
                $CurrentYear--;
            }
        ?&gt;
        </select>

rather than having 100 lines of <option>'s.

I was able to get set_select to work fine for the month values because they are not in a loop. I'm sure it's just something silly that I've done, but the above code does not keep the selected value when the form is submitted and fails validation.

Does anyone have any suggestions? Thanks in advance!
#2

[eluser]gtech[/eluser]
this is the set_select in the validation code
Code:
function set_select($field = '', $value = '')
    {
        if ($field == '' OR $value == '' OR  ! isset($_POST[$field]))
        {
            return '';
        }
            
        if ($_POST[$field] == $value)
        {
            return ' selected="selected"';
        }
    }

so quick check when the form is displayed, view source on the browser and see if you get the ' selected="selected" ' appear in your desired option. If not you could just echo $_POST['BirthYear'] on the form page and see what it is set to, it might help you debug the problem.
#3

[eluser]Dave S[/eluser]
I tried some more debugging and it is returning the correct year to the controller. I'm wondering if it is because I am using the variable $CurrentYear in set_select. Maybe what I'm trying to do isn't possible?

If I just hard code a few years in instead of the loop, it works fine so I suspect the variable.
#4

[eluser]gtech[/eluser]
what I would try in your situation

Code:
<select name="BirthYear">
    <option value="">Year</option>
    &lt;?PHP
        $CurrentYear = date("Y");
        $num_times_set = 0;
        while ($CurrentYear >= $StartYear) {
            // I will take out '' around $CurrentYear just to see if that helps
            echo "<option value=\"$CurrentYear\"".$this->validation->set_select('BirthYear', $CurrentYear).">$CurrentYear</option>";

            $res = $this->validation->set_select('BirthYear', $CurrentYear);
            if ($res != '') {
              $num_times_set = $num_times_set + 1;
            }
            $CurrentYear--;
        }
        
    ?&gt;
</select>
&lt;?php echo "NUMTIMES SET=".$num_times_set."<BR>"?&gt;

if you set the select more than once that may be a problem I am not sure... what does NUMTIMES SET equal ?
#5

[eluser]Dave S[/eluser]
Ah hah! It was the single quotes around $CurrentYear !

Told you it was something silly I did Smile

Thanks for your help!
#6

[eluser]gtech[/eluser]
I should of spotted that straight away, ah well glad its working.
#7

[eluser]achilez[/eluser]
i also met this kind of error.

is "birthdate" a reserve word in code igniter?
#8

[eluser]achilez[/eluser]
I have an update validation form, my BirthMonth, BirthDay, and BirthYear content field will get the data from the database. My problem is that there's no action (won't validate/freeze) when i access the accountinfo_form function and reached into "BirthYear" field. Although BirthMonth and BirthDay are working, im just confused is "BirthYear" a reserve/special work or something?

Hope you can help me with this problem, pls see my code below.

Code:
&lt;?

    function accountinfo_form() {
        $this->load->library('validation');
        $this->load->helper('form');
        
        //rules
        $rules['firstname'] = "trim|required";
        $rules['lastname'] = "trim|required";
        $rules['birthmonth'] = "trim|required";
        $rules['birthday'] = "trim|required";
        $rules['birthyear'] = "trim|required";
        $this->validation->set_rules($rules);

        //fields
        $fields['firstname'] = 'First Name';
        $fields['lastname']    = 'Last Name';
        $fields['birthmonth'] = 'Month';
        $fields['birthday'] = 'Day';
        $fields['birthyear'] = 'Year';
        $fields['address1'] = 'Address1';
        $fields['zipcode'] = 'Zip Code';
        $fields['city'] = 'City';
        $fields['state'] = 'State';
        $fields['country'] = 'Country';
        $this->validation->set_fields($fields);

           if(isset($_POST['form_trigger'])){
            if($this->validation->run() == TRUE){
                //update the account information in My Profile
                //update the database once the validation is true
                $tag['accountinfo_updated'] = '<h3 align="center">Your Account Information was successfully updated!</h3>
                                                <p align="center"><a href="dashboard/index">Back to Dashboard</a></p>';
            }
        } // end isset
        
        $attributes = array('id' => 'frmaccountinfo');
        $tag['begin_form'] = form_open('profile/accountinfo', $attributes);


        $sql = "SELECT * FROM tbl_months";
        $query = $this->db->query( $sql );
        $birthmonthdb[''] = 'Select Month';
        foreach( $query->result() as $birthmonth ){
            $birthmonthdb[$birthmonth->code] = $birthmonth->name;
        }
        $tag['birthmonth']= form_dropdown('birthmonth', $birthmonthdb, ($this->validation->run())?$this->validation->birthmonth:$this->common->extractdatemonth("birthdate",0), 'id="birthmonth"');
        $tag['birthmonth_error'] = $this->validation->birthmonth_error;


        $sql = "SELECT * FROM tbl_days";
        $query = $this->db->query( $sql );
        $birthdaydb[''] = 'Select Day';
        foreach( $query->result() as $birthday ){
            $birthdaydb[$birthday->code] = $birthday->name;
        }
        $tag['birthday']= form_dropdown('birthday', $birthdaydb, ($this->validation->run())?$this->validation->birthday:$this->common->extractdatemonth("birthdate",1), 'id="birthday"');
        $tag['birthday_error'] = $this->validation->birthday_error;
        
        
        $sql = "SELECT * FROM tbl_years";
        $query = $this->db->query( $sql );
        $birthyeardb[''] = 'Select Year';
        foreach( $query->result() as $birthyear ){
            $birthyeardb[$birthyear->name] = $birthyear->name;
        }
        $tag['birthyear']= form_dropdown('birthyear', $birthyeardb, $this->validation->birthyear, 'id="birthyear"');
        $tag['birthyear_error'] = $this->validation->birthyear_error;

        $tag['submit'] = form_submit('submit', 'Update Profile');
        $tag['end_form'] = form_hidden('form_trigger', 'true').form_close();
        
        return $this->parser->parse('accountinfo_form.php', $tag, true);
    }
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB