问题描述
|
这是我的场景,我正在使用codeigniter创建一个表单,我了解如何使用模型等填充字段。我有表格的布局。现在从我的索引函数运行。我想存储所有提供给该表单的数据,并在postdata数组中访问它们,每个索引都是值的名称。请帮忙。 CodeIgniter,PHP
解决方法
您创建表格
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作为第二个参数传递。查看输入库上的用户指南
,参见代码点火器的输入类
如何形成代码的一个例子是:
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
}
这样,您的表单将被显示,如果提交的内容将被处理,如果发生错误,您将能够保留提交的值以在表单中预先输入它们,显示错误消息等...如果有效用户被重定向到他可以安全地重新加载页面而无需提交多次。