CodeIgniter Forums

Full Version: Re-Populate form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]robert.fulcher[/eluser]
I am slowly going through and learning how to repopulate forms. I have one item that I can't figure out. I am grabbing the id from a uri to pull the data for an edit form. I setup the rules to re-populate the data when they don't fill in a field that they need to. The only thing I need to figure out is how to I create the uri to pass to the model.

Model:

Code:
function get_vendor()
   {
      $id = $this->uri->segment(4);
      $query = $this->db->query("SELECT id, code, name, parent, created FROM vendors where id = ".$id);
      return $query;
   }

vendor edit is a submit not a uri...

Controller:

Code:
class Vendor_edit_c extends Controller{

   function index()
   {
      $this->output->enable_profiler(TRUE);
      $this->load->model('Vendor_model','',TRUE);
      $data['query'] = $this->Vendor_model->get_vendor();
      $data['parents'] = $this->Vendor_model->parent_list();
      $this->load->view('vendor/vendor_edit',$data);        
   }

   function submit()
   {  
      $this->form_validation->set_rules('id', 'id');
      $this->form_validation->set_rules('code', 'Code', 'required');
      $this->form_validation->set_rules('name', 'Name');
      $this->form_validation->set_rules('parent', 'Parent');
      
      if ($this->form_validation->run() == FALSE)
        {
           $this->load->model('Vendor_model','',TRUE);
                   $data['query'] = $this->Vendor_model->get_vendor(); (error on this line because no uri with ID in it.)
                   $data['parents'] = $this->Vendor_model->parent_list();
                   $this->load->view('vendor/vendor_edit',$data);      
        }
        else
        {
                   $this->load->model('Vendor_model','',TRUE);
                   $this->Vendor_model->update_vendor();
                   redirect("vendor/vendor");
      }
   }  

}

Thanks for any help.

El Forum

[eluser]The Wizard[/eluser]
when you POST a form, i think there will be no uri at all that is posted.

when you are serving the form, that is the function index,
you should try to add some hidden fields in the form that keep the URI.

then you process the form, that is submit, you should read the hidden fields
as if they were normal post fields, and process it like this, that should solve
your problem.

El Forum

[eluser]robert.fulcher[/eluser]
herrkaleun,

I understand what you are saying and I do have a hidden field. Below is my edit form:

Code:
<html >
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />      
    <title>Title</title>    
</head>
<body>

<h1>Edit Vendor</h1>
&lt;?php echo form_open('vendor/vendor_edit_c/submit'); ?&gt;
<table border=1>
&lt;?php foreach($query->result() as $vendor):?&gt;
   &lt;?php echo form_hidden('id', set_value('id',$vendor->id)); ?&gt;
   <tr>
      <th>Company Code</th>
      <td>&lt;?php echo form_input('code', set_value('code',$vendor->code)); ?&gt;&lt;?php echo form_error('code'); ?&gt;</td>
   </tr>
   <tr>
      <th>Company Name</th>
      <td>&lt;?php echo form_input('name', set_value('name',$vendor->name)); ?&gt;</td>
   </tr>
   &lt;!--
   <tr>
      <th>Parent Company Code</th>
      <td>&lt;?php echo form_input('parent', $vendor->parent); ?&gt;</td>
   </tr>
   --&gt;
   <tr>
      <th>Parent Company Code</th>
      <td>&lt;?php echo form_dropdown('parent', $parents['parents'], set_value('parent', $vendor->parent) ,'default'); ?&gt;</td>
   </tr>
   <tr>
      <td colspan=2>
          &lt;?php echo form_submit('submit', 'Submit'); ?&gt;
      </td>
   </tr>
&lt;?php endforeach;?&gt;
</table>
&lt;?php echo form_close(); ?&gt;
<br><br><br><br>

&lt;?php
    $this->load->view('footer');
?&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;?php

print_r($parents);
echo "<br><BR></br>";
$test = $parents['parents'];
print_r($test);


?&gt;

I gest my question now is should I move my uri parsing code from the model to the controller and pass the id. Then I can handle it the same in both the index and the submit. I just want to make sure I do it the proper way.

El Forum

[eluser]The Wizard[/eluser]
why not pass the uri as parameter TO the function instead of letting the function it figure it out by itself?

El Forum

[eluser]robert.fulcher[/eluser]
I am not sure I understand. I thought that the uri was automatically being passed. I did not know that I needed to pass it manually. I am assuming that we are both referring to the index() procedure.

Otherwise how would I create and pass a URI in the submit function? Also currently the Index function and the uri being passed works fine, is it not supposed to work that way?

Thanks

El Forum

[eluser]The Wizard[/eluser]
hmm nice approach Smile
do not know if it is passed automaticly, why not try it?