这是我的场景,我正在创建一个带有codeigniter的表单,我理解如何使用模型等填充字段。我有表格的布局。它现在从我的索引函数运行。我想存储给该表单的所有数据,并在postdata数组中访问它们,每个索引都是值的名称。请帮忙。 CodeIgniter,PHP
答案 0 :(得分:0)
您创建表单
echo form_open('mycontroller/mymethod');
// rest of form functions
or <form name="myform" method="post" action="<?php echo site_url('mycontroler/mymethod');?>" > // rest of html form
then, in Mycontroller:
function mymethod()
{
$postarray = $this->input->post();
// you can pass it to a model to do the elaboration , see below
$this->myloadedmodel->elaborate_form($postarray)
}
Model:
function elaborate_form($postarray)
{
foreach($postarray as $field)
{
\\ do your stuff
}
}
如果您想要XSS过滤,可以在$this->input->post()
调用中传递TRUE作为第二个参数。查看user guide on input library
答案 1 :(得分:0)
如何构建代码的一个例子是:
public function add_something()
{
if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') { // if the form was submitted
$form = $this->input->post();
// <input name="field1" value="value1 ... => $form['field1'] = 'value1'
// you should do some checks here on the fields to make sure they each fits what you were expecting (validation, filtering ...)
// deal with your $form array, for exemple insert the content in the database
if ($it_worked) {
// redirect to the next page, so that F5/reload won't cause a resubmit
header('Location: next.php');
exit; // make sure it stops here
}
// something went wrong, add whatever you need to your view so they can display the error status on the form
}
// display the form
}
通过这种方式,您的表单将被显示,如果提交,其内容将被处理,如果发生错误,您将能够保持提交的值在表单中预先输入,显示错误消息等...并且如果它有效,则用户被重定向到他可以安全地重新加载页面而不需要多次提交。