doumengwei0138 2012-12-27 07:47
浏览 88
已采纳

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

How can I pass the data input by user from view to controller in codeigniter Php using get or post method? I'm currently new to codeigniter..thanks!

AddProduct.php (my view)

<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 (my Controller)

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条回答 默认 最新

  • dongpang2029 2012-12-27 13:24
    关注

    I have created controller named : Test_controller and view named : manage_test_controller and using below code you can get data from view file to 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(); ?>
    

    Hope this code will help you.... :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化