开发者

help storing postdata with codeigniter

开发者 https://www.devze.com 2023-03-08 08:52 出处:网络
Here is my scenario, I am creating a form with codeigniter, I understand how to popula开发者_StackOverflowte the fields with models and such.I have the layout of the form.It is now running from my ind

Here is my scenario, I am creating a form with codeigniter, I understand how to popula开发者_StackOverflowte the fields with models and such. I have the layout of the form. It is now running from my index function. I want to store all the data given to that form and access them in a postdata array with each index being the name of the value. Please help. CodeIgniter, PHP


you create the form

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
    }
  }

If you want XSS filtering you can pass a TRUE as second parameter in the $this->input->post() call. Check user guide on input library


See code igniter's input class

One exemple of how to form your code would be:

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
  }

This way your form will be displayed and if submitted its content will be processed, if an error occurs you will be able to keep the submitted values to pre-enter them in the form, display an error message etc ... And if it works the user is redirected to that he can reload the page safely without submitting multiple times.

0

精彩评论

暂无评论...
验证码 换一张
取 消