2017-08-23 60 views
0

我是codeigniter的新手,我目前正在註冊表單上工作。目前我只想讓用戶輸入他們的名字和圖片。但是,我有2個不同的控制器來處理這個問題,我不知道如何將它們合併到一個表單中,而沒有兩個不同的提交按鈕。合併兩種形式codeigniter

查看

<div class="container"> 
    <div class="row"> 
     <section class="page col-md-8"> 

     <h2 class="page-title">Registration Form</h2> 


     <form method="post" id="expertiseForm" action="" > 

     <?php echo $this->session->flashdata('msg'); ?> 

      <?php $attributes = array("name" => "expertiseform"); 
      echo form_open("index.php/expertise/index", $attributes);?> 

       <div class="form-group required"> 
        <label class="control-label" for="fname">First Name&#160;</label> 
        <input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" /> 
        <span class="text-danger"><?php echo form_error('fname'); ?></span> 
       </div> 

       <div class="form-group"> 
        <button type="submit" class="btn btn-default" value="form-submit'">Submit</button> 
       </div> 

     <?php echo form_close(); ?> 
     </form> 


     <!-- Upload form --> 
     <?php echo form_open_multipart('index.php/upload/do_upload', array('id' => 'uploadForm'));?> 

      <div class="form-group required"> 
      <label class="control-label" for="filephotograph">Photograph:&#160;</label> 
      <input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>"> 
      <small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small> 
      <span class="text-danger"><?php echo form_error('filephotograph'); ?></span> 
      </div> 

      <input type="submit" value="upload" /> 

     <?php echo form_close(); ?> 
     </form> 
    </section> 
    </div> 
</div> 

控制器的名字

<?php 
class Expertise extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->database(); 
     $this->load->model('expertise_model'); 
    } 

    function index() 
    { 
     $this->load->view('templates/navbar'); 
     $this->load->view('templates/header'); 

     $this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[2]|max_length[30]|xss_clean'); 


     if ($this->form_validation->run() == FALSE) 
     { 
     // fails 
      $this->load->view('expertise_form'); 
      $this->load->view('templates/footer'); 
     } 

     else { 

      $data = array(
       'fname' => $this->input->post('fname') 
       ); 

      if ($this->expertise_model->insert_expertise($data)) 
      { 
       $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You have successfully submit your expertise form for review.</div>'); 
       redirect('index.php/expertise/index'); 
      } 
      else 
      { 
      // error 
       $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Error. Please review your information and try again.</div>'); 
       redirect('index.php/expertise/index'); 
      } 
     } 
    } 
?> 

控制器上傳照片

<?php 

class Upload extends CI_Controller { 

     public function __construct() 
     { 
       parent::__construct(); 
       $this->load->helper(array('form', 'url')); 
     } 

     public function index() 
     { 
       $this->load->view('upload_form', array('error' => ' ')); 
     } 

     public function do_upload() 
     { 

       $config['upload_path']   = './application/CAHSIfiles/uploads/photos'; 
       $config['allowed_types']  = 'gif|jpg|png'; 
       $config['max_size']    = 100; 
       $config['max_width']   = 1024; 
       $config['max_height']   = 768; 

       $this->load->library('upload', $config); 

       if (! $this->upload->do_upload('filephotograph')) 
       { 
         $error = array('error' => $this->upload->display_errors()); 
         redirect('index.php/expertise/index'); 
       } 
       else 
       { 
         $data = array('upload_data' => $this->upload->data()); 
         redirect('index.php/expertise/index'); 
       } 
     } 
} 
?> 

回答

1

只是有一組與URL將到處理您的第一組形式的項目之一函數形式的標籤,然後進行實際的上傳邏輯

<?php echo form_open_multipart('index.php/index.php/expertise/proccess_profile', array('id' => 'uploadForm'));?> 

    <div class="form-group required"> 
     <label class="control-label" for="fname">First Name&#160;</label> 
     <input class="form-control" name="fname" type="text" value="<?php echo set_value('fname'); ?>" /> 
     <span class="text-danger"><?php echo form_error('fname'); ?></span> 
    </div> 

    <div class="form-group required"> 
     <label class="control-label" for="filephotograph">Photograph:&#160;</label> 
     <input type="file" class="form-control-file" id="filephotograph" name="filephotograph" aria-describedby="fileHelp" value="<?php echo set_value('filephotograph'); ?>"> 
     <small id="fileHelp" class="form-text text-muted">Upload a photograph smaller than 2 MB in size.</small> 
     <span class="text-danger"><?php echo form_error('filephotograph'); ?></span> 
     </div> 

    <div class="form-group"> 
     <button type="submit" class="btn btn-default" value="form-submit'">Submit</button> 
    </div> 



<?php echo form_close(); ?> 

控制器

public function process_profile(){ 
    //logic from your upload controller and other controller here 
} 
+1

對不起,我花了太長時間給我一個答覆,我花了一段時間才弄明白,但我管理它。謝謝! –