Welcome Guest, Not a member yet? Register   Sign In
populate form from database
#1

[eluser]deltrem[/eluser]
In the controller, I have...

function update($id = null)
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->database();
$data = array();
$data = $this->db->get_where(
'users',
array(
'id' => $id
)
);
$data = $data->result_array();
$data = $data[0];
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|matches[emailconf]');
$this->form_validation->set_rules('emailconf', 'Email Confirmation', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->run();
this->load->vars($data);
$this->load->view('update');
}


In the view, I have...

<h5>Username</h5> &lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;

<h5>Email</h5> &lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;

<h5>Email Confirmation</h5> &lt;input type="text" name="emailconf" value="&lt;?php echo set_value('emailconf'); ?&gt;" size="50" /&gt;

<h5>Password</h5> &lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirmation</h5> &lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;


I have seen set_value() reading $data at

http://ellislab.com/forums/viewthread/103837/


What am I doing wrong?
#2

[eluser]Dam1an[/eluser]
I'm not too sure about the structure of your code there, the normal logic using the form_validation class is
Code:
// Massivly simplified login controller
public function login() {
    $this->load->library('form_validation');
    
    // Set the rules
    $this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|sha1');

    if($this->form_validation->run() == FALSE) {
        // Load the register form
        $this->load->view('auth/login');
    } else {
        // Validation passed, process the data
    }
}

The view is pretty much the same as yours

Also, whya re you passing the data array to the view, you never seem to use it? Or am I missing something

Oh, and can you please use [ code ] blocks next time Smile
#3

[eluser]Thorpe Obazee[/eluser]
Code:
&lt;input type=“text” name=“username” value=”&lt;?php echo set_value(‘username’, content_from_db); ?&gt;” size=“50” /&gt;

You need the second parameter of the set_value helper.
#4

[eluser]deltrem[/eluser]
thanks a lot! it works!




Theme © iAndrew 2016 - Forum software by © MyBB