使用Codeigniter从视图获取数据到控制器

Lhy*_*ynx 2 php codeigniter

如何使用get或post方法将用户从视图输入的数据传递到codeigniter Php中的控制器?我现在是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>
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

}

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);
}
Run Code Online (Sandbox Code Playgroud)

Siv*_*ada 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>
Run Code Online (Sandbox Code Playgroud)

观察行动的变化

并在你的comtroller指定的方法获取值,如下所示

$id=$this->input->post('id')
$idescription=$this->input->post('description')
Run Code Online (Sandbox Code Playgroud)

然后将这些发送到模型并执行您想要的任何操作

加载url helper库使其$this->baseurl()工作使用其他明智的硬代码localhost/path...

  • `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). (2认同)