• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
making a form to work

#1
[eluser]entropy[/eluser]
I have a big problem with making a form work.

I use a controller to handle a registration request. When I strip de controller down to this:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

public function index() {
$this->load->view('top');
$this->load->helper('form');
$this->load->view('end');
}
}

?>
the 'top' and 'end' views are neatly shown. But when I make a start with showing a form:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

public function index() {
$this->load->view('top');
$this->load->helper('form');
echo form_open('register/confirm');
$data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => '',
              'maxlength'   => '40',
              'size'        => '40'
            );
echo form_input($data);    
$this->load->view('end');
}
}

?>
CI then places the form and input field at the very top of the html, even before the <html> tag.

It seems to me that making a form with the form helper is just impossible. Either that, or else: What am I doing wrong?

#2
[eluser]entropy[/eluser]
I found the solution. The form code must be placed in a view that is loaded by the controller.

controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {

   public function index() {
     $this->load->view('top');
     $this->load->view('register');
     $this->load->view('end');
   }
}

?>

view:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
     $this->load->helper('form');
     echo form_open('register/confirm');
     $data = array(
              'name'        => 'username',
              'id'          => 'username',
              'value'       => '',
              'maxlength'   => '40',
              'size'        => '40'
            );
     echo form_input($data);
?>

#3
[eluser]PhilTem[/eluser]
You're absolutely right: Your every code for a view (what is to be displayed on the client side) must be put in a view and loaded with

Code:
$this->load->view('path/to/view', $data);

to display it at the proper position.
Your code posted in the OP doesn't work because of the way CI_Loader::view() works: It starts a buffer and includes your view file, then flushing the buffer to get the content and setting as the output-response. If you use echo (which, by the way, you should never use in a controller - that's what views are made for) in your controller it will not get caught by the buffer and since the final outputs is passed to the client as the last thing done your code will appear on the very top of the generated source thus before the <head>-tag Wink

#4
[eluser]CroNiX[/eluser]
All CI output is buffered (views). If you echo from a controller, that gets output before the views get sent out, no matter the order in the code (all echos go out realtime, all views get sent out at the end of processing). The only way around that is to manually set the output (instead of echo) using the output class in the controller, or just use views.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.