Welcome Guest, Not a member yet? Register   Sign In
How to set old() to the dropdown menu ?
#1

(This post was last modified: 09-04-2023, 11:24 PM by sknair143.)

Hello,
I'm building form where user can submit some inputs. I check for validation errors and re display the form with error messages and also i use <?=old('filed_name')?> to display old entered data.

Everything works for input box but, i have dropdown box where user cans select gender, But how we can show the old data on this dropdown.

My code (Textbox) :

Code:
<input type="text" name="first_name" value="<?=old('first_name')?>" id="firstName">

My code (Dropdown) : 

PHP Code:
  <select id="defaultSelect" value="<?= old('gender') ?>" name="gender" class="form-select">
                                    <option value="">Select</option>
                                    <option value="Male">Male</option>
                                    <option value="Female">Female</option>

                                </select

But this dropdown not working with old selected values.
Reply
#2

(This post was last modified: 09-04-2023, 11:30 PM by sammyskills.)

You will need to use the set_select() form helper. See docs

PS: If you are using any of the form_open() helpers (form_open, form_open_multipart), you do not need to use old(). Use set_value() instead.
Reply
#3

(This post was last modified: 09-05-2023, 12:15 AM by sknair143.)

(09-04-2023, 11:27 PM)sammyskills Wrote: You will need to use the set_select() form helper. See docs

PS: If you are using any of the form_open() helpers (form_open, form_open_multipart), you do not need to use old(). Use set_value() instead.

Thanks. I'm using form helper but i tried this , but not working

PHP Code:
<select id="defaultSelect" name="nationality_id" class="form-select">
                                    <option value="">Select</option>

                                    <?php foreach ($nationalities as $nation): ?>
                                        <option value="<?= $nation['id'?><?= set_select('nationality_id'?>><?= $nation['name'?></option>
                                    <?php endforeach; ?>
                                </select> 
Reply
#4

(This post was last modified: 09-05-2023, 12:23 AM by sammyskills.)

You need to add the value to check for after the field name, like so:

PHP Code:
<option name="<?= $nation['id'] ?>" <?= set_select('nationaility_id'$nation['id']) ?>><?= $nation['name'?></option> 
Reply
#5

(09-05-2023, 12:23 AM)sammyskills Wrote: You need to add the value to check for after the field name, like so:

PHP Code:
<option name="<?= $nation['id'] ?>" <?= set_select('nationaility_id'$nation['id']) ?>><?= $nation['name'?></option> 

Yes it works Smile

Thank you, I'm learning CI
Reply




Theme © iAndrew 2016 - Forum software by © MyBB