简体   繁体   中英

Codeigniter form, redirect page on submit

I have created a search form that I want to forward on submit.

The way it should work is the user enters the term/keyword they want to search for, the form will then forward them to /search/$keyword

My research has let me to the conclusion that the form needs to be posted to the controller then do the forwarding from there. But I have tried numerous times and can't get it to forward.

Here is the form:

<?php echo form_open('search'); ?>
            <input id="search_text" name="searchquery" type="text" value="enter your search here..." onfocus="if(this.value == 'enter your search here...') this.value='';" onblur="if(this.value == '') this.value='enter your search here...';" maxlength="120" >
       <?php echo form_close();?>

And the controller:

if($this->input->post('searchquery')){
 redirect('search', $this->input->post('searchquery'));
}

change this line:

redirect('search', $this->input->post('searchquery'));

to:

redirect('search/' . $this->input->post('searchquery'));

In your redirect call, the second parameter is the "method" of redirection, so that's why your redirect is not working. what you want is something like this:

redirect('search/my+search+term');

That's why we do the string concatenation, instead of passing the search term to the second parameter.

Try this instead.

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

You were using the redirect method incorrectly. Here's an excerpt from the documentation.

The optional second parameter allows you to choose between the "location" method (default) or the "refresh" method. Location is faster, but on Windows servers it can sometimes be a problem

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