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

时间:2012-12-27 07:47:19

标签: 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>

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);
}

2 个答案:

答案 0 :(得分: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')

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

加载url帮助程序库,使$this->baseurl()使用localhost/path...

使用其他明智的硬代码

答案 1 :(得分:4)

我创建了名为:Test_controller的控制器和名为:manage_test_controller的视图,使用下面的代码可以将视图文件中的数据传输到控制器。

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(); ?>

希望这段代码可以帮助你.... :)