2012-12-27 82 views
2

如何使用get或post方法將用戶從視圖中輸入的數據傳遞給codeigniter Php中的控制器?我目前是新的codeigniter ..謝謝!使用Codeigniter從視圖獲取數據到控制器

AddProduct.php(我的視圖)

<body> 
    <form method="POST" action="SaveProductController"></br></br></br> 
     <table border='1' align='center'> 
      <tr> 
       <td>ID: </td><td><input type="text" name="id" 
             value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td> 
      </tr> 
      <tr> 
       <td>Description: </td><td><input type="text" name="description"></td> 
      </tr> 
      <tr> 
       <td></td><td><input type="submit" name="addProduct" value="Add Product"><td> 
      </tr> 
     </table> 
    </form> 
</body> 

SaveProductController .PHP(我的控制器)

class SaveProductController extends CI_Controller{ 

function index($description){ 
    $this->load->model('ProductDao'); 
    $data['id'] = $this->id; 
    $data['description'] = $this->description; 
    print_r($data); 
    //$this->ProductDao->saveProduct(); 
} 

}

ProductDao.php

function saveProduct() { 
    $data = array(
    'id' => $this->input->xss_clean($this->input->post('id')), 
    'description' => $this->input->xss_clean($this->input->post('description')), 
    'price' => $this->input->xss_clean($this->input->post('price')), 
    'size' => $this->input->xss_clean($this->input->post('size')), 
    'aisle' => $this->input->xss_clean($this->input->post('aisle')), 
    ); 

    $query = $this->db->insert('mytable', $data); 
} 
+0

什麼問題呢? – EbinPaulose

+0

http://stackoverflow.com/questions/2279282/how-to-process-a-form-with-codeigniter –

回答

4

我已經創建控制器名爲:Test_controller和視圖名爲:manage_test_controller和d使用下面的代碼,您可以將數據從視圖文件傳輸到控制器。

Test_controller.php

class Test_controller extends CI_Controller { 

     var $controller = "user"; 
     var $formValues = array(); 
    function manage_user() {    

       $this->formValues['formAction'] = SITEURL . '/' . 
       $this->controller . '/manage_' . $this->controller; 

       if (isset($_POST['displayName'])) 
        $this->formValues['displayName'] = $_POST['displayName']; 
       else 
        $this->formValues['displayName'] = ""; 
       if (isset($_POST['userEmail'])) 
        $this->formValues['userEmail'] = $_POST['userEmail']; else 
        $this->formValues['userEmail'] = "";     

      $this->load->view('header'); 
      $this->load->view($this->controller . '/manage_' . 
      $this->controller, $this->formValues); 
      $this->load->view('footer'); 
     } 
    } 

Manage_test_controller.php

<?php echo form_open_multipart($formAction); ?> 
<table> 
    <tr> 
      <td><?php echo form_label('Display Name'); ?><em>*</em></td> 
      <td><?php echo form_input('displayName',$displayName); ?></td> 
     </tr> 
    <tr> 
      <td><?php echo form_label('Email'); ?><em>*</em></td> 
      <td><?php echo form_input('userEmail',$userEmail); ?></td> 
     </tr> 
</table> 
<?php echo form_close(); ?> 

希望這個代碼將會幫助你.... :)

5
<body> 
    <form method="POST" action="<?php echo $this->base_url();?>/controllername/methodname"></br></br></br> 
     <table border='1' align='center'> 
      <tr> 
       <td>ID: </td><td><input type="text" name="id" 
             value="<?php echo $GetProductId->id + 1; ?>" readonly="readonly"></td> 
      </tr> 
      <tr> 
       <td>Description: </td><td><input type="text" name="description"></td> 
      </tr> 
      <tr> 
       <td></td><td><input type="submit" name="addProduct" value="Add Product"><td> 
      </tr> 
     </table> 
    </form> 
</body> 

觀察行動

,並在您comtroller指定的方法來獲取值的變化如下圖所示

$id=$this->input->post('id') 
$idescription=$this->input->post('description') 

然後把這些模型,做你想要什麼都

負載網址助手庫提$this->baseurl()工作其他明智的硬代碼它使用localhost/path...

+1

+1,當你使用'index'函數時你不需要表單action中的方法名,默認情況下,索引方法將啓動。 –

+2

另外'base_url'是一個輔助函數,不需要使用'$ this',檢查[url helper](http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html)和[form helper] (http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html)。 –

+0

@Sivagopal Manpragada嗨。謝謝你的幫助。我已成功在我的頁面中顯示數據。如果你不介意,這條線意味着什麼? 'action =「<?php echo $ this-> baseurl();?>/controllername/methodname」>'..我已經配置baseurl on/application/config/config/baseurl ='index.php' – Lhynx

相關問題