简体   繁体   中英

php codeigniter controller not performing action after post

I have a form that posts a search query to my 'search' controller:

<div id="search_box">
<?php echo form_open('search'); ?>
<?php echo form_input('searchvalue', 'search...'); ?>
<?php echo form_submit('submit', 'Search!'); ?>
<?php echo form_close(); ?>
</div>

In my search controller I have the following code:

  public function index()
    {
           $page = 'search';
        $category = 'search';

         if($this->input->post('searchvalue')) {
                  redirect('search/query');
         };


......

}

My problem is that it won't do the redirect. I have the form helper autoloaded. What can I do to solve this mystery. Is it a case for the batman?

In Codeigniter if you ask for a POST variable from the Input class it will return the value or FALSE if the value is empty (Personal Experience) or not found.

So Maybe instead put in a Hidden field with a Dummy value and just check for that on each submit. You can then perform validation and redirect. This will then work if the search query is provided or not.

And the ; after you if is not valid PHP.

For Example:

<div id="search_box">
<?php echo form_open('search'); ?>
<?php echo form_hidden('mysearchform', 'true'); ?>
<?php echo form_input('searchvalue', 'search...'); ?>
<?php echo form_submit('submit', 'Search!'); ?>
<?php echo form_close(); ?>
</div>

public function index() {

$page = 'search';
$category = 'search';

if($this->input->post('mysearchform') != FALSE) {

    // Remember to Validate your Query

    redirect('search/query');
}

}

Hope I understood that correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM