【问题标题】:Getting data from view to controller using Codeigniter使用 Codeigniter 从视图获取数据到控制器
【发布时间】:2012-12-12 15:21:26
【问题描述】:

如何使用 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);
}

【问题讨论】:

标签: php codeigniter


【解决方案1】:
<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>

观察动作变化

并在您的控制器指定的方法中获取如下所示的值

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

然后将这些发送给模型并做任何你想做的事情

加载 url 帮助程序库以使 $this-&gt;baseurl() 使用 localhost/path... 进行其他明智的硬编码

【讨论】:

  • +1,当你使用index函数时,表单动作中不需要方法名,默认会启动索引方法。
  • 还有base_url是辅助函数,不用$this,检查url helperform helper
  • @Sivagopal Manpragada 嗨。谢谢你的帮助。我已成功在我的页面中显示数据。如果你不介意,这条线是什么意思? action="&lt;?php echo $this-&gt;baseurl();?&gt;/controllername/methodname"&gt; ..我已经在 /application/config/config/baseurl = 'index.php' 上配置了 baseurl
  • localhost/codeigniter/index.php/ 这部分叫做baseurl() 阅读base_url()的codeigniter手册你能接受我的回答吗
【解决方案2】:

我创建了名为: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(); ?>

希望这段代码对您有所帮助.... :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    相关资源
    最近更新 更多