CodeIgniter Forums
Forms - set_value driving me mad! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Forms - set_value driving me mad! (/thread-57089.html)



Forms - set_value driving me mad! - El Forum - 02-13-2013

[eluser]35mm[/eluser]
Creating login and register forms. The login form works fine and set_value works.

The registration form, set_value will not work and I've spent far to long trying to work out why! It will display the validation errors but not the set_value. If I enter a username and click submit, I get the validation error that no user name was entered. So it seems the username isn't even getting through to the controller. Saying that has just made me look at this in a different way - it's not set_value that isn't working. It's the whole form that isn't working! But why?

Here's the controller code (I've removed all but username validation to save space);
Code:
public function submit()
    {
      
            
            $this->load->helper(array('form', 'url'));
            $this->load->library('form_validation');
            
  $this->form_validation->set_rules('username', 'username', 'required');

  if ($this->form_validation->run() == FALSE)
  {
                    $this->config->load('config');
                    $site_name = $this->config->item('site_name');
                    $data=array('title'=>'Join ' . $site_name);
                    
                    $this->load->view('pages/register_form',$data);
  }
Then in the view I have this (relevant bits only;
Code:
echo form_open('register/submit');
    echo validation_errors();
    // user name
    $user_name = array(
              'name'        => 'username',
              'id'          => 'username',
              'maxlength'   => '25',
              'size'        => '25',
              'value'       => set_value('username'),
            );

    echo form_label('Username ', 'username') . form_input($user_name) . '<br />';



Forms - set_value driving me mad! - El Forum - 02-14-2013

[eluser]Harold Villacorte[/eluser]
I was never able to get it to work that way. It will work like this though and with less code:
Code:
&lt;?php echo form_open('register/submit') ;?&gt;

<label for="username">Username:</label>
&lt;input type="text" name="username" value="&lt;?php echo set_value('username') ;?&gt;" /&gt;

<label for="password">Password:</label>
&lt;input type="password" name="password" value="" /&gt;

&lt;?php echo form_submit('submit', 'Login') ;?&gt;

&lt;?php echo form_close() ;?&gt;



Forms - set_value driving me mad! - El Forum - 02-14-2013

[eluser]35mm[/eluser]
Ah ok. Funny thing is I was trying to echo it in the page to, but that wasn't working either. It now works thanks to you. Cheers!