views:

27

answers:

1

Hi,

Using CodeIgniter, how do I access and display text entered into an input field on a view file (see code below) from my controller file?

 // input_view.php 
<?php 
     echo form_open('search/submit');   
     $input_data = array('name' => 'search_field', 'size' =>  '70');
     echo form_input($input_data);
     form_submit('submit','Submit');
     form_close(); 
?>
+1  A: 

When text is entered into an input field, it is impossible to access until the form has been posted back to the server. In other words, the form must be submit for your controller to see it.

Let's say you have a form in the file called input_view.php:

<?php echo form_open('my_controller/my_method'); ?>
<?php echo form_input('search'); ?>
<?php echo form_submit('submit', 'Search'); ?>

When this form is submit, it will be sent to the 'my_controller' controller.

Now, Here's what the my_method should look like if you want to simply print the contents of the search field:

public function my_method() {
  if ($this->input->post()) {
    $name = $this->input->post('search');
    echo $name;
  }
}

I hope this helps.

DexterW
+1.Thank you Greelmo for your helpful reply. When I enter text and click the submit button, for some reason, if($this->input->post()) evaluates to false and the code does not execute. I am trying to figure out why its not getting the text I inputted
01010011
@Greelmo, my logic seems to be sketchy right now because when I changed if($this->input->post()==FALSE) it printed what I input. I don't understand.
01010011