6

In the controller, I have...

function update($id = null)
{
    $this->load->database();

    // more code

    $data = array();
    $data = $this->db->get_where(
            'users',
            array(
            'id' => $id
            )
        );
    $data = $data->result_array();
    $data = $data[0];

    // more code

    $this->load->view('update', $data);
}

In the view, I have...

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

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

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

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

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

set_value() isn't reading $data

search for value=""

at http://codeigniter.com/forums/viewthread/103837/

The poster uses only the set_value() function between "" in value="".

I'm wondering how to do the same, but I can't get it to work. Help?

2
  • 1
    Why are you pulling data in the controller instead of the model? Commented Nov 29, 2009 at 20:39
  • 3
    because he (we) can... and want to. ;) Commented Apr 18, 2011 at 4:17

5 Answers 5

21

set_value requires 2 parameters. field name AND value.

You need:

value="<?php echo set_value('username', $username); ?>"

assuming that you are passing $data['username'] etc.

Sign up to request clarification or add additional context in comments.

2 Comments

This is the correct answer. It takes care of the case of prepopulating the form from the database + populating form validation user input
Its correct, but one more thing will save you tons of time if you replace your System/helpers/set_value code like suggested here codeigniter.com/forums/viewthread/159535/#775628 to avoid problems when repopulating form again when rule doesnt exist.
4

I think you are not passing the value correctly to the view.

In the Controller put:

$data = $this->db->get_where(
        'users',
        array(
        'id' => $id
        )
    );
$data = $data->result_array();

$data['result'] = $data[0];

then inside the view:

<h5>Password Confirmation</h5> <input type="text" name="passconf" value="<?php echo $result['passconf']; ?>" size="50" />

Comments

3

Try this:

Controller:

$data = array();
$data = $this->db->get_where(
        'users',
        array(
        'id' => $id
        )
    );
$data = $data->result_array();
$data['view_username'] = $data['username'];

Instead of using $date[0], I use the column name, but that's your decision

View:

<h5>Username</h5> <input type="text" name="username" value="<?=$view_username?>" size="50"/>

<?= => is PHP short tag, read about it in the codeigniter user guide

Regards,

Sylvio

Comments

1

set_value() is designed to work with CI form validation.

When the form is first loaded, set_value() can fill the field with a default value using its second parameter. After the form is submitted, you can display the form again and the field will be filled with the value that was submitted providing it was checked for validation.

See also http://codeigniter.com/forums/viewthread/96617/ - there are a few gotchas with set_value() that the docs don't clarify.

Applicable up to CI 1.7.3 so far.

Comments

-1

Try to assign default values to $_POST variable in your controller, e.g. $_POST['email'] = 'email';

1 Comment

You can't set any $_POST values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.