Add string from form input to URL using Codeigniter
I have a div containing a search box:
<div id="searchbox">
<?php echo form_open('places/search/sort_by/id/sort_order/desc/term/'.$this->input->post('term')); ?>
<?php echo form_label('Search for', 'term'); ?>
<?php echo form_input('term', set_value('term'), 'id="term"'); ?>
<?php echo form_submit('submit', 'Search'); ?>
<?php echo form_close(); ?>
</div>
In the input the user can type in a search term, which will be given to the controller using $this->input->post('term') which then passes to the model to query the database. Now after typging in the search term and submitting the form, I want the url to end with the search term ie. places/search/sort_by/id/sort_order/desc/term/dinosaur if 'dinosaur' was the search term. How can I do this?
Right now the code above passes the POST to controller, but the url right after submitting the form is just places/search/sort_by/id/sort_order/desc/term/. Then if the user searches for another term say 'cats', after submitting the url becomes places/search/sort_by/id/sort_order/desc/term/dinosaur when the search term is actually 'cats'.
How can I solve this?
In other words, this is the behavior i want
- URL is places/search/sort_by/id/sort_order/desc/term/ and search box is empty
- 'Dinosaur' is typed into search box, and press submit
- URL is now places/search/sort_by/id/sort_order/desc/term/dinosaur and search box contains 'dinosaur'
- 'cats' is typed into search box, and press submit
- URL is now places/search/sort_by/id/sort_order/desc/term/cats and search box contains 'cats'
This is what I am getting with the above c开发者_如何学Pythonode
- URL is places/search/sort_by/id/sort_order/desc/term/ and search box is empty
- 'Dinosaur' is typed into search box, and press submit
URL is now places/search/sort_by/id/sort_order/desc/term/ and search box contains 'dinosaur'
'cats' is typed into search box, and press submit
- URL is now places/search/sort_by/id/sort_order/desc/term/dinosaur and search box contains 'cats'
Try this...
<div id="searchbox">
<?php echo form_open('places/search/sort_by/id/sort_order/desc/term/'); ?>
<?php echo form_label('Search for', 'term'); ?>
<?php echo form_input('term', set_value('term', $term), 'id="term"'); ?>
<?php echo form_submit('submit', 'Search'); ?>
<?php echo form_close(); ?>
</div>
Then in the controller that accepts it...
// Note the default NULL value for the last uri segment
// |
// v
function search($sort_by, $id, $sort_order, $desc, $term, $search_term = NULL)
{
// if $search_term is null redirect on itself concating the "post" data
// on the end of the uri
if ($search_term === NULL)
{
redirect('the/same/uri/plus/the/term/'.$this->input->post('term'));
}
// load up this variable to fill the input
$data['term'] = $search_term;
//then do whatever
}
精彩评论