How to process a form with CodeIgniter


Question

I am new to CodeIgniter. I need to process a form. I have a form.html page in view

<html>
  <head>
    <title>Search</title>
  </head>
  <body>
    <form action="search">
      <input type="text" name="search" value="" size="50" />
      <div>
        <input type="submit" value="Submit" />
      </div>
    </form>
  </body>
</html>

and form controller

class Form extends Controller {

  function Form() {
    parent::Controller();   
  }

  function index() {    
    $this->load->view('form');
  }

}

and I have an view file search.php but when it is processed it shows page not found...

1
13
2/18/2010 4:24:39 PM

In M.odel V.iew C.ontroller setups like CodeIgniter the Views are user interface elements. They should not be parsing results.

If I am not mistaken, what you are looking to do is pass data from www.yoursite.com/index.php/form to www.yoursite.com/index.php/search

In unstructured php you might have a form.html with a form action of search.php. A user would navigate to yoursite.com/form.html, which would call yoursite.com/search.php, which might redirect to yoursite.com/results.php.

In CodeIgniter (and, as far as I understand it, in any MVC system, regardless of language) your Controller, Form calls a function which loads the form.html View into itself and then runs it. The View generates the code (generally HTML, but not necessarily) which the user interacts with. When the user makes a request that the View cannot handle (requests for more data or another page) it passes that request back to the Controller, which loads in more data or another View.

In other words, the View determines how the data is going to be displayed. The Controller maps requests to Views.

It gets slightly more complicated when you want to have complex and / or changing data displayed in a view. In order to maintain the separation of concerns that MVC requires CodeIgniter also provides you with Models.

Models are responsible for the most difficult part of any web application - managing data flow. They contain methods to read data, write data, and most importantly, methods for ensuring data integrity. In other words Models should:

  • Ensure that the data is in the correct format.
  • Ensure that the data contains nothing (malicious or otherwise) that could break the environment it is destined for.
  • Possess methods for C.reating, R.eading, U.pdating, and D.eleting data within the above constraints.

Akelos has a good graphic laying out the components of MVC:

Request - Response
(source: akelos.org)

That being said, the simplest (read "easiest", not "most expandable") way to accomplish what you want to do is:

function Form()
{
    parent::Controller();   
}

function index()
{   
        $this->load->view('form');
}

function search()
{
        $term = $this->input->post('search');
        /*
            In order for this to work you will need to 
            change the method on your form.
            (Since you do not specify a method in your form, 
            it will default to the *get* method -- and CodeIgniter
            destroys the $_GET variable unless you change its 
            default settings.)

            The *action* your form needs to have is
            index.php/form/search/
        */

        // Operate on your search data here.
        // One possible way to do this:
        $this->load->model('search_model');
        $results_from_search = $this->search->find_data($term);

        // Make sure your model properly escapes incoming data.
        $this->load->view('results', $results_from_search);
}
32
6/17/2019 7:05:50 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon