CodeIgniter Forums
Basic Form question - just can't recall how! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Basic Form question - just can't recall how! (/showthread.php?tid=1186)



Basic Form question - just can't recall how! - blackbulldog - 02-17-2015

Hi,
Got a simple 'edit' style form which, when submitted, gets validated. If there are errors then I want to redisplay the form with whatever the user entered ... can;t for the life of me recal how to do the redisplay!!!

Here's the controller which loads the form:
Code:
public function edit_staff($par_id)
{
$this->data['staff'] = $this->M_staff->get_selected($par_id);
$alldepts=$this->M_department->get_all();
$this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts);
$this->data['title'] = 'staff Management';
$this->data['message'] = $this->session->flashdata('message');
$this->data['main'] = 'v_staff_edit';
$this->load->view('v_template', $this->data, 'refresh');
}
Here's the controller that validates it and 'should'  do the redisplay
Code:
public function edit_staff_submit()
{
if ($this->form_validation->run('staff') == true)
{
$data = array(
'fname'=> $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'dept'=> $this->input->post('dept')
);
$this->M_staff->update_record($this->input->post('staffid'),$data);
$this->session->set_flashdata('message', "<p>staff updated successfully.</p>");
redirect('staff/index', 'refresh');
}
else  // If validation fails, r.edisplay
{
What to do here, how do i redisplay view?????
}

}

Here's the view:

Code:
   <div class="row">
       <div class="col-md-6 col-md-offset-3 well">
       <?php
       echo form_open("staff/edit_staff_submit/");
       echo form_hidden('staffid', $staff->staffid);
        ?>
       <fieldset>
           
           <div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="fname" class="control-label">First Name</label>
</div>
<div class="col-md-8">
<input id="fname" name="fname" placeholder="fname" type="text" class="form-control"  value="<?php echo set_value('fname', $staff->fname); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
           </div>
           <div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="fname" class="control-label">Last Name</label>
</div>
<div class="col-md-8">
<input id="lname" name="lname" placeholder="Last name" type="text" class="form-control"  value="<?php echo set_value('lname', $staff->lname); ?>" />
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>

           <div class="form-group">
<div class="row colbox">
<div class="col-md-4">
<label for="Department" class="control-label">Department</label>
</div>
<div class="col-md-8">
<?php echo form_dropdown('dept', $dropdown, $staff->dept);?>
<span class="text-danger"><?php echo form_error('fname'); ?></span>
</div>
</div>
</div>


</div>
</div>          

           <div class="form-group">
           <div class="col-sm-offset-4 col-md-8 text-left">
               <input id="btn_update" name="btn_update" type="submit" class="btn btn-primary" value="Update" />
               <?php echo anchor('staff/index','<input id="btn_cancel" name="btn_cancel" type="reset" class="btn btn-danger" value="Cancel">')?>
           </div>
           </div>
       </fieldset>
       <?php echo form_close(); ?>
       <?php echo $this->session->flashdata('msg'); ?>
       </div>
   </div>



RE: Basic Form question - just can't recall how! - paralogizing - 02-17-2015

Are you looking for the set_value() helper function?


RE: Basic Form question - just can't recall how! - blackbulldog - 02-17-2015

(02-17-2015, 12:32 PM)paralogizing Wrote: Are you looking for the set_value() helper function?

The form already uses the set_value helper. My question is what needs to happen to make that function show what the user entered, what code needs to go in the 'validation failed' case?


RE: Basic Form question - just can't recall how! - Avenirer - 02-17-2015

As I see it, you have no validation rules set: you do a form_validation->run() but without the validating rules. To redisplay the view, you should take the staff id from the hidden form element, and with it, you should do the same as you've done in the edit_staff method.


RE: Basic Form question - just can't recall how! - blackbulldog - 02-17-2015

(02-17-2015, 01:06 PM)Avenirer Wrote: As I see it, you have no validation rules set: you do a form_validation->run() but without the validating rules. To redisplay the view, you should take the staff id from the hidden form element, and with it, you should do the same as you've done in the edit_staff method.

The form validation is loaded from the config file.

Surely just copying the code from the other controller isn't the way to go, is it? And a simple redirect to the edit form controller will simply overwrite what the user entered, won't it?


RE: Basic Form question - just can't recall how! - Avenirer - 02-17-2015

Well... I would have done everything in one method...

PHP Code:
public function edit_staff($par_id)
{

    
$par_id = ($this->input->post('staffid')) ? (int) $this->input->post('staffid') : (int) $par_id;

    if(
$this->form_validation->run('staff') === FALSE)
    {
        
$this->data['staff'] = $this->M_staff->get_selected($par_id);
        
$alldepts=$this->M_department->get_all();
        
$this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts);
        
$this->data['title'] = 'staff Management';
        
$this->data['message'] = $this->session->flashdata('message',"<p>There were errors in your form</p>");
        
$this->data['main'] = 'v_staff_edit';
        
$this->load->view('v_template'$this->data); 

    }

    else
    {
        
$data = array(
        
'fname'=> $this->input->post('fname'),
        
'lname' => $this->input->post('lname'),
        
'dept'=> $this->input->post('dept')
        );
        
$this->M_staff->update_record($this->input->post('staffid'),$data);
        
$this->session->set_flashdata('message'"<p>staff updated successfully.</p>");
        
redirect('staff/index''refresh');
    }


And the form would just send to the same page:

PHP Code:
<div class="row">
 
      <div class="col-md-6 col-md-offset-3 well">
 
      <?php
       
echo form_open(); // it will send the form data to same page
 
      echo form_hidden('staffid'$staff->staffid);
 
       ?>
....and all the form elements that come after these... 

I hope it helped you.


RE: Basic Form question - just can't recall how! - blackbulldog - 02-18-2015

(02-17-2015, 01:40 PM)Avenirer Wrote: Well... I would have done everything in one method...


PHP Code:
public function edit_staff($par_id)
{

 
$par_id = ($this->input->post('staffid')) ? (int) $this->input->post('staffid') : (int) $par_id;

 if(
$this->form_validation->run('staff') === FALSE)
 {
 
$this->data['staff'] = $this->M_staff->get_selected($par_id);
 
$alldepts=$this->M_department->get_all();
 
$this->data['dropdown'] = $this->M_department->build_dept_dropdown($alldepts);
 
$this->data['title'] = 'staff Management';
 
$this->data['message'] = $this->session->flashdata('message',"<p>There were errors in your form</p>");
 
$this->data['main'] = 'v_staff_edit';
 
$this->load->view('v_template'$this->data); 

 }

 else
 {
 
$data = array(
 
'fname'=> $this->input->post('fname'),
 
'lname' => $this->input->post('lname'),
 
'dept'=> $this->input->post('dept')
 );
 
$this->M_staff->update_record($this->input->post('staffid'),$data);
 
$this->session->set_flashdata('message'"<p>staff updated successfully.</p>");
 
redirect('staff/index''refresh');
 }


And the form would just send to the same page:


PHP Code:
<div class="row">
 
      <div class="col-md-6 col-md-offset-3 well">
 
      <?php
       
echo form_open(); // it will send the form data to same page
 
      echo form_hidden('staffid'$staff->staffid);
 
       ?>
....and all the form elements that come after these... 

I hope it helped you.

Avenirer,

Thanks mate, that's exactly what I was looking for .


RE: Basic Form question - just can't recall how! - Avenirer - 02-18-2015

Anytime. Hope you enjoy using CodeIgniter. Smile