views:

563

answers:

4

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

+3  A: 

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.

Donny Kurnia
A: 

Try using the codeigniter 'site_url' in your action to make sure you are pointing to the right place. The action in your example would have gone to the 'search' controller rather than the 'form' controller.

<html>
<head>
<title>Search</title>
</head>
<body>
<form action="<?= site_url('form/process_search') ?>">
<input type="text" name="search" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>

index is only used in your controller when nothing else is passed.. So in the case of my example above you would want something like this:

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

function process_search()
{   

     print "<pre>";

     print_r($_POST);

     print "</pre>";
}
angryCodeMonkey
A: 

Nettuts has a great tutorial for CodeIgniter for Login form. Follow the screencast and it will clear up your questions.

http://net.tutsplus.com/videos/screencasts/codeigniter-from-scratch-day-6-login/

Yada
+2  A: 

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

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);
}
Sean Vieira
+1 -- My Optimistic Upvote. It's clear and inviting. I'm not skilled enough to evaluate how true it is, but I'm gonna see if I can use it to become so.
Smandoli