php 使用 Codeigniter 从视图获取数据到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14051231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting data from view to controller using Codeigniter
提问by Lhynx
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!
如何使用 get 或 post 方法将用户输入的数据从视图传递到 codeigniter Php 中的控制器?我目前是 codeigniter 的新手..谢谢!
AddProduct.php (my view)
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 (my Controller)
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
产品道.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);
}
回答by Sivagopal Manpragada
<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>
observe the change in action
观察动作的变化
and in your comtroller specified method get values as shown below
并在您的控制器指定的方法中获取如下所示的值
$id=$this->input->post('id')
$idescription=$this->input->post('description')
then send these to model and do what ever you want
然后将这些发送给模型并做任何你想做的事
load url helper library to make $this->baseurl()to work other wise hard code it using localhost/path...
加载 url 帮助程序库以使其$this->baseurl()使用其他明智的硬编码localhost/path...
回答by Yogesh Pingle
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 的控制器和名为:manage_test_controller 的视图,使用下面的代码,您可以从视图文件中获取数据到控制器。
Test_controller.php
测试控制器.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
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.... :)
希望这段代码能帮助你.... :)

