Welcome Guest, Not a member yet? Register   Sign In
re-populating data in the form for the SELECT tag
#1

[eluser]Unknown[/eluser]
Hi gys in the CI documentation they had given a clear document for re-populating the form data after the submit button is pressed to all input types (form)
in Form Validation section except the SELECT, CHECKBOX & RADIO options. if any one knows how to repopulate the data for the SELECT option / RADIO/CHECKBOX just reply me.

Thanx in advance for your golden reply.....


Reagrds

Kumar
#2

[eluser]Evil Wizard[/eluser]
use the the "form_helper"
Code:
<?=form_radio(array('name'=>'radio1','id'=>'radio1_y', 'value'=>'y', 'checked'=>set_radio('radio1', 'y', TRUE)));?>
<?=form_radio(array('name'=>'radio1','id'=>'radio1_n', 'value'=>'n', 'checked'=>set_radio('radio1', 'n')));?>

<?=form_checkbox(array('name'=>'chk1','id'=>'chk1', 'value'=>'n', 'checked'=>set_checkbox('chk1', 'n')));?>

there are examples here:
http://ellislab.com/codeigniter/user-gui...elper.html
#3

[eluser]Unknown[/eluser]
Hi thanx 4 the reply,

i cant get wat u r trying to say,

my problem is ....

consider the situation

1. open form
2. insert a text box named "txtname"
3. insert a combo box named "country"
- India
- Australia
- UK
- Srilanka
4. insert a radio option for "gender"
- Male
- Female
5. insert a check box for "items"
- Mobile
- Laptop
6. insert a submit button
7. close form

After creating the above said form,

when i press submit button

it has to validate the POST data

if some datas are invalid

then it should show error, [ I M GETTING ERROR ]

after displaying the error, the old data which was entered in the text box is

displaying using the set_value() function

but i m not getting the re populated value for

combo box, radio option and the check box.

just send me a code where the things are working properly....

Thanx 4 the reply...

---------------------------------

view File

myform.php


Code:
<html>
<head>
<title>My Form</title>
</head>
<body>


&lt;?php //echo validation_errors('<div style="color: #FF0000; background:#F3CBCE;">','</div>'); ?&gt;

&lt;?php echo form_open('../form'); ?&gt;

<h5>Username</h5>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;&lt;?php echo form_error('username','<div style="color: #FF0000; background:#F3CBCE;">','</div>'); ?&gt;

<h5>Password</h5>
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;&lt;?php echo form_error('password','<div style="color: #FF0000; background:#F3CBCE;">','</div>'); ?&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;&lt;?php echo form_error('passconf','<div style="color: #FF0000; background:#F3CBCE;">','</div>'); ?&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;&lt;?php echo form_error('email','<div style="color: #FF0000; background:#F3CBCE;">','</div>'); ?&gt;

<select name="myselect">
<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>


&lt;input type="checkbox" name="mycheck[]" value="1" &lt;?php echo set_checkbox('mycheck[]', '1'); ?&gt; /&gt;Red
&lt;input type="checkbox" name="mycheck[]" value="2" &lt;?php echo set_checkbox('mycheck[]', '2'); ?&gt; /&gt;Green

&lt;input type="radio" name="myradio" value="1" &lt;?php echo set_radio('myradio', '1', TRUE); ?&gt; /&gt;Radio1
&lt;input type="radio" name="myradio" value="2" &lt;?php echo set_radio('myradio', '2'); ?&gt; /&gt;Radio2


<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;



------------------------------------

Controller File


form.php

Code:
&lt;?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|max_length[12]|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
                
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
    function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}
?&gt;
#4

[eluser]LuckyFella73[/eluser]
I got to your post while searching for something and maybe it's too late
to answer now but just in case:

"set_select" and "set_checkbox" in you view-file does not work because you
didn't set the rules for that element in your controller.
#5

[eluser]George Tavas[/eluser]
I am suggesting the use of javascript to repolulate a form instead the use of the validation library.

I have created jquery plugin for it. If anyone intrested just contact me.
#6

[eluser]quest13[/eluser]
Can you send me the jquery validation library for repopulating the form ?

Thank you

my email id : [email protected]
#7

[eluser]gmahesha[/eluser]
Hello friend, can i get the jquery validation library for repopulating the form ?
my email id is [email protected]

Thanks in advance.




Theme © iAndrew 2016 - Forum software by © MyBB