Welcome Guest, Not a member yet? Register   Sign In
Prefilling my select menu and Radio button
#1

[eluser]kikz4life[/eluser]
Hi to all,

I have a problem, I have a select menu and a radio button in my form.
What I want is when I update my form I want it to prefill the select menu and radio button after I successfully saved my data. Tried adding (TRUE) 3rd parameter in my select and radio but this doesn't solve it.

Code:
<select name="course">
    <option value=""></option>
        &lt;?php
        foreach($course as $item)
        {
        echo "<option value='".$item->data_id."'".set_select('course',$item->data_id).">[".$item->data_value."] ".$item->data_display."</option>";//to study
        }
        ?&gt;
</select>&lt;?php echo form_error('course');?&gt;

<div class="rleft">Gender:</div>
    &lt;input type="radio" name="gender" value="Male" &lt;?php echo set_radio('gender','Male');?&gt;/&gt;Male
    &lt;input type="radio" name="gender" value="Female" &lt;?php echo set_radio('gender','Female');?&gt;/&gt;Female
&lt;?php echo form_error('gender')?&gt;<br />


Here, I've attached a screen shot.
#2

[eluser]LuckyFella73[/eluser]
Did you set up rules (form validation) for the elements you
want to repopulate after submitting the form?
#3

[eluser]kikz4life[/eluser]
yup, i used form_validation config files. I am able to show errors if the validation is invoke. But my problem is I can't show the data's updated in a select menu and radio button. In simple &lt;input form&gt; I am able to do this.

by the way my select menu is equal to this except I didn't include a 3rd parameter:
Code:
<select name="myselect">
<option value=""></option>
<option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
<option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
<option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>
</select>
#4

[eluser]LuckyFella73[/eluser]
I guess there is something wrong in your controller.
I took your select code (copy/ paste) and did set up a test
controller - everything worked fine.

Code:
// controller
&lt;?php
class Test extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
    }

    function index()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('myselect', 'myselect', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form');
        }
        else
        {
            $this->load->view('form');
        }
    }
}

// view / test.php
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Form Test&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<h1>TEST FORM</h1>
&lt;form action="&lt;?php echo base_url();?&gt;test" method="post"&gt;

<select name="myselect">
    <option value=""></option>
    <option value="one" &lt;?php echo set_select('myselect', 'one', TRUE); ?&gt; >One</option>
    <option value="two" &lt;?php echo set_select('myselect', 'two'); ?&gt; >Two</option>
    <option value="three" &lt;?php echo set_select('myselect', 'three'); ?&gt; >Three</option>
</select>
<br />
&lt;input type="submit" name="sub" value="send" /&gt;

&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
#5

[eluser]kikz4life[/eluser]
thank you for the quick response LuckyFella73,

what I'm saying is that I have no problem throwing error message when my form validation invoked. But what I'm trying to do is like this: If i have a radio button for Male and Female if I chose the Female then what I want to see is the Female radio button is enabled. Same goes for my select menu. But don't know how to do this. I don't think adding a 3rd parameter TRUE will do the trick. Based from what I think that adding a 3rd parameter is just like a setting default button but what if I choose other options. Sorry if my explanation is a bit messy (^_^)
#6

[eluser]LuckyFella73[/eluser]
I thought what you want is selecting some entry from your dropdown (select),
submitting the form, process data, display the form again with the previous
made selections. And that is working like I posted, at least I did the check
for the select field. Sorry if I missunderstood the question.
#7

[eluser]kikz4life[/eluser]
again thank you for your response. (^_^)

from my first post what I used to populate my select menu came from my database table dataset so if add a 3rd parameter TRUE, i think the logic is not correct cause I'm using a foreach loop. It would just set all my options to TRUE. Am I right?

Code:
foreach($course as $item)
        {
        echo "<option value='".$item->data_id."'".set_select('course',$item->data_id).">[".$item->data_value."] ".$item->data_display."</option>";//to study
        }

also in my radio button even if i chose female, the radio button is still enabled in the Male?. (>.<)
Code:
<div class="rleft">Gender:</div>
    &lt;input type="radio" name="gender" value="Male" &lt;?php echo set_radio('gender','Male',TRUE);?&gt;/&gt;Male
    &lt;input type="radio" name="gender" value="Female" &lt;?php echo set_radio('gender','Female');?&gt;/&gt;Female
&lt;?php echo form_error('gender')?&gt;<br />
#8

[eluser]sstalder[/eluser]
I think I have narrowed it down, take a look at this example:

Code:
class Test extends CI_Controller
{
    function index()
    {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('myselect', 'myselect', 'required');
        
        if ($this->form_validation->run() == FALSE)
        {
            $data['myselect'] = '';
        }
        else
        {
            $data = $_POST;
        }
        
        $this->load->view('form', $data);
    }
}

Code:
&lt;form action="&lt;?php echo current_url();?&gt;" method="post"&gt;

    <select name="myselect">
        <option value=""></option>
        <option value="one"&lt;?php echo set_select('myselect', 'one'); ?&gt;>One</option>
        <option value="two"&lt;?php echo set_select('myselect', 'two'); ?&gt;>Two</option>
        <option value="three"&lt;?php echo set_select('myselect', 'three'); ?&gt;>Three</option>
    </select>
    <br />
    &lt;input type="submit" name="sub" value="send" /&gt;

&lt;/form&gt;

It seems like with the form_validation library loaded the values don't persist across a postback however if you set the data manually as I did above you get the expected behavior.
#9

[eluser]harpster[/eluser]
Not sure if this helps but this is what I do and the radios stick...


Code:
&lt;?php echo form_radio("gender", "Male", (set_value("gender") == "Male") ); ?&gt; Male &nbsp;
&lt;?php echo form_radio("gender", "Female", (set_value("gender") == "Female") ); ?&gt; Female &lt;?php echo form_error('gender'); ?&gt;

with this validation rule in the controller

Code:
$this->form_validation->set_rules('gender', 'Gender', 'required');




Theme © iAndrew 2016 - Forum software by © MyBB