在codeigniter中加载视图两次

在codeigniter中加载视图两次

问题描述:

Hello I just started CodeIgniter. I am having problem in loading view.

My scenrio is I am creating a sample add form. After submit it goes to controller and insert entries in database. I want if the database operation is successfull it again comes to same view having again some values. And on the basis of those values I am showing some particular rows informing user about insertion operation. My function in controller looks like

public function add_user()
{
    $this->load->view('add_user');
    $post=$this->input->post();
    if(isset($post['name']))
    {
        $data=array(
        'name'=>$post['name'],
        'designation'=>$post['designation']
        );
        if($this->db->insert('user',$data))
        $result['update']=true;
        else
        $result['update']=false;
        $this->load->view('add_user',$result);
    }

}

And my view looks like

    <h1 align="center">Add User</h1>
    <table border="0" cellpadding="2" cellspacing="2" align="center">
        <?php
        if(isset($update))
        {
            if($update)
            {
                ?>
                <tr bgcolor="#00FF00">
                    <td>Record Added Successfully</td>
                </tr>
            <?php
            }
            else
            {
                ?>
                <tr bgcolor="#FF0000">
                    <td>Insertion Operation Failed</td>
                </tr>
            <?php
            }   
        }
        ?>
        <?php echo(form_open('first/add_user'));?>
            <tr>
                <td>Name</td>
                <td><input type="text" name="name" /></td>
            </tr>
            <tr>
                <td>Designation</td>
                <td>
                    <select name="designation">
                        <option value="Junior PHP Developer">Junior PHP Developer</option>
                        <option value="Senior PHP Developer">Senior PHP Developer</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" value="Add User" />
                </td>
            </tr>   
        </form>
    </table>

Now What I want that if insertion operation is successfull I am sending true value to view and if not I am sending false value. And on the basis of this value I am showing some rows. I am loading the view two times as per I understood the logic. Because first time it loads the form and second time It loads view with some value of true or false. But what happens that after it reloads there are two forms. I know this problem is due to double loading of my view. i want to ask if there is another way of sending values after database operation to view?

Simply load your view once:

public function add_user()
{
  $post=$this->input->post();
  $result = array();
  if(isset($post['name']))
  {
      $data=array(
       'name'=>$post['name'],
       'designation'=>$post['designation']
      );
      if($this->db->insert('user',$data))
      $result['update']=true;
      else
      $result['update']=false;
  }
  $this->load->view('add_user',$result);
}

By the way your code is a bit messy, work on it

// try something like this
//you may need to use form validation helper 

//load add user form
public function add_user(){
//any data required for the form
$data['anything'] = '';
$this->load->view('add_user',$data);
}
//to process adding user action, form action will point to this
function adding_user(){
 if($this->input->post('name')){
   $data=array(
   'name'=>$post['name'],
   'designation'=>$post['designation'];
    if($this->db->insert('user',$data)){
     echo 'user added successfully!';
    }else{
     redirect(user/add_user);
    }
  );

 }
}