Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to process a form with CodeIgniter

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

like image 753
Ramesh Avatar asked Feb 17 '10 08:02

Ramesh


People also ask

Why form is not submitting in CodeIgniter?

Make sure you load your form helper and url helper. Make sure you use form validation when submitting form in codeigniter on controller. Also you should upgrade to the new bootstrap I see your using old version.

How do I validate a form in CI?

Setting Validation Rules CodeIgniter lets you set as many validation rules as you need for a given field, cascading them in order, and it even lets you prep and pre-process the field data at the same time. To set validation rules you will use the set_rules() method: $this->form_validation->set_rules();

What is form helper in CodeIgniter?

Form Helper files basically contains functions which are needed to create different segments of a form (e.g inputbox , submitbutton , dropdown boxes etc.) in CodeIgniter. To use these functions it is needed to load a form helper library.


2 Answers

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);
}
like image 121
Sean Vieira Avatar answered Sep 28 '22 04:09

Sean Vieira


View file is useless without the controller to load and displaying it. You must create a controller to receive the form data, process it, then displaying the process result.

You can use a form helper to set the form open tags, also the close tags:

<?php echo form_open('form/search'); ?>
<input type="text" name="search" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
<?php echo form_close(); ?>

Without using form helper, you can still write it this way:

<form action="<?php echo site_url('form/search'); ?>">

Then add the search method into form controller:

function search()
{
  //get form field
  $search = $this->input->post('search');
  // do stuffs here
  //...
}

Remember that CI only help you with the basic code organization and provide a helpful library and helper. But you still need to write the algorithm of the process in your site.

Don't forget to read the included user guide in the downloaded codeigniter package. You can learn many stuffs from the example in there. Don't hesitate to ask things you don't know here, many member of stackoverflow will help you.

like image 43
Donny Kurnia Avatar answered Sep 28 '22 04:09

Donny Kurnia