使用codeigniter帮助存储postdata


help storing postdata with codeigniter

这是我的场景,我正在用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
  }

通过这种方式,您的表格将被显示,如果提交,其内容将被处理,如果发生错误,您将能够保留提交的值,以便在表格中预先输入它们,显示错误消息等。。。如果它有效,用户会被重定向到,他可以安全地重新加载页面,而无需多次提交。