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

[eluser]KeyStroke[/eluser]
Hi,

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
#2

[eluser]Popcorn[/eluser]
Code:
set_value('field_name');
#3

[eluser]KeyStroke[/eluser]
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.
#4

[eluser]RS71[/eluser]
Have you looked at the User guide?

Havent tried but it could be something like this:

Code:
<?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)
#5

[eluser]therealmaloy[/eluser]
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

Model

Code:
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->where('user_id',$this->user->user_id);
            $this->db->update($this->users_details_table, $details_fields);            
        }else {
            $this->db->insert($this->users_details_table,$details_fields);
        }
        
    }



Controller

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

        $config = array(
                
                    array(
                    'field'=>'name',
                    'label'=>'First Name',
                    'rules'=>'trim|strip_tags|xss_clean|max_length[50]|required'),
        
                    array(
                    'field'=>'surname',
                    'label'=>'Sur Name',
                    'rules'=>'trim|strip_tags|xss_clean|max_length[50]|required'),
        
                    array(
                    'field'=>'location',
                    'label'=>'Present Location',
                    'rules'=>'trim|strip_tags|xss_clean|max_length[50]'),
        
                    array(
                    'field'=>'gender',
                    'label'=>'Gender',
                    'rules'=>'trim|max_length[1]'),

        
        ...// **************  cut codes
        
        
        $this->form_validation->set_rules($config);


        
        //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 :)


            $this->template->display('member/profile/edit',$data);
        
        }else{

            //validation complaint user details, saved the information
            
            redirect('member/profile/edit/'.$mode);
        }
        
    }



View

Code:
...//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;
            
                            </div>

                            <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>
                            <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>
                          
                          
                            <p><label for="has_children">Has Children? </label>
                                &lt;?php
                                
                                    $lists = array(
                                            '0'=>'[Children]',
                                            '1'=>'No',
                                            '2'=>'Yes'
                                            );
                                            

                                    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>';
                                        
                                    }
                                    
                                    echo('</select>');
                                    
                                ?&gt;
                            </p>
                        
...//more codes
#6

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

[eluser]KeyStroke[/eluser]
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!!
#8

[eluser]underskor[/eluser]
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:
Code:
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
            $this->page_model->fetch_page();

            //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