Welcome Guest, Not a member yet? Register   Sign In
How do populate AND re-populate a form?


I have an "Edit" form that populates itself with information from the database. I need it to also repopulate itself in case of error with the user entered values. How do I do that efficiently?

I know how to only do one of them at a time, but not both. And I don't want to hack my way through it neither.

Appreciate your help Smile


As I said, I need to populate it initially with values from the database, too. That will only repopulate the form in case of error.

Have you looked at the User guide?

Havent tried but it could be something like this:

<?php echo form_dropdown($birthday_day_field, $birthday_day_options, set_value($birthday_day_field, (isset($databasevalue->fetch)) ? $databasevalue->fetch : ''), $birthday_day_params); ?>

(the set_value part and its second param)

These are snippets from one of my present project, this might be a little bit lengthy but this can surely help you out with your problem, i hope Smile

Major chunks in each codes were stripped for readability purposes. I may have cut structures and/or you might see non-properly closed constructs, sorry for that Smile


function get_user_details($user_id){
        // **************  get the user details


    function add_update_details(){

        // ************** inserts or updates the details

        $details_fields = array(
                'user_id'        =>$this->user->user_id,
                'gender'        =>$this->input->post('gender'),
                'birth_date'        =>$birth,
                'country_origin'    =>$this->input->post('country_origin'),

        ...// ************** cut codes

                'vital_statistics_or_built' => $this->input->post('vital_statistics_or_built')
        if ($this->has_details($this->user->user_id)){
            $this->db->update($this->users_details_table, $details_fields);            
        }else {


function edit(){
        $this->template->metas('title', 'Member Backend :: Profile :: Edit');
        $this->load->library('form_validation'); //CI 1.70

        $config = array(
                    'label'=>'First Name',
                    'label'=>'Sur Name',
                    'label'=>'Present Location',

        ...// **************  cut codes

        //pre-propagate for the view to get contents from the database
        $data['details'] = $this->usersdetails->get_user_details($this->user->user_id);

        if ($this->form_validation->run() == FALSE){

            //user details, will be propagated to the view at first run and/or validation failure
            //this is where the trick usually comes in :)


            //validation complaint user details, saved the information


...//more codes

                            <h2>Manage your profile</h2>

                            <div class="form_error" style="color: red; line-height: 8px; font-size: 11px;">
                                &lt;?php echo validation_errors(); ?&gt;

                            <p><label for="height">Height: </label>
                                &lt;?=form_input(array('type'=>'text','name'=>'height','value'=> !is_null( $details) ? $details->height : set_value('height'),'id'=>'height'));?&gt;
                            <p><label for="weight">Weight: </label>
                                &lt;?=form_input(array('type'=>'text','name'=>'weight','value'=> !is_null( $details) ? $details->weight : set_value('weight'),'id'=>'weight'));?&gt;
                            <p><label for="has_children">Has Children? </label>
                                    $lists = array(

                                    echo('<select name="has_children">');
                                    foreach ($lists as $key => $value){
                                        echo '<option value="'.$key.'" ';
                                        echo set_select('has_children', $key, (!is_null( $details ) ? $details->has_children : '0') == $key ? TRUE :FALSE);
                                        echo '>'.$value.'</option>';
...//more codes

[eluser]Chris Williams[/eluser]
I used this for my form (god I hate forms so much) and it worked great. Thanks!

I found the solution!

set_value() actually has an additional parameter for the "default" value of the field. I just used that to initially populate the form and it works perfectly now!!

I read your first post and it describes what I was trying to do yesterday, though mine is more to do with query errors and less to do with input validation. For what it's worth, here is my solution:
function edit_page() {
        //Set page content variables
        $this->page_model->page_title = 'Example';
        $content['current_page'] = $this->page_model->page_title;
        //Test if form has been submitted
        if($this->input->post('submit') === FALSE) {
            //Fetch page data for editing
            //Returns DB results to $this->page_model->page_body

            //Set form view variable to data fetched from DB
            $form_data['page_body'] = $this->page_model->page_body;
            //Load edit_form as string, passing it the $form_data array
            $content['page_form'] = $this->load->view('page_edit_form', $form_data, TRUE);
            //Load view, replacing view variables with content
            $this->load->view('page_edit', $content);
        } else {
            //Set class var to user input, run upate_page() and return success bool
            $this->page_model->page_body = $this->input->post('page_body');
            $update_success = $this->page_model->update_page();
            //If update is successful, display appropriate output in view
            if ($update_success !== FALSE) {
                $content['edit_status_msg']     = 'Update Successful';
                $this->load->view('page_edited', $content);

            //If not, display editing view with submitted data so they can retry
            } else {
                $form_data['page_body'] = $this->input->post('page_body');
                $content['page_form'] = $this->load->view('page_edit_form_error', $form_data, TRUE);
                $content['edit_status_msg']     = 'Update Failed';
                $content['edit_status_desc'] = 'An error has occured.';
                $this->load->view('page_edit', $content);

The only difference between the page_edit_form and page_edit_form_error views is that _error has a <div>Error Message</div> at the top.

Hope that helps someone.

Theme © iAndrew 2016 - Forum software by © MyBB