Welcome Guest, Not a member yet? Register   Sign In
how to create a www.example.com/search/keyword keyword is $_POST['search']; anyone?
#1

[eluser]dudel[/eluser]
Hello everybody Smile,

So, I'm new and I'm stuck with uri routing.

Basicaly, all I want to do is add a keyword after the search in the url.

I`ve put this two lines into index function of the controller :
Code:
$query = rawurlencode($this->input->post('search'));
redirect('/search/'.$query);

and this line in the routes file
Code:
$route['search/?(.*)'] = 'search/$1';

I have a form on www.example.com. when i type something in the input text i want it to appear after the "search" in the url. like this www.example.com/search/keyword.

Does anyone have any suggestions? Thank you.
#2

[eluser]richthegeek[/eluser]
You can use a mod_rewrite rule to redirect (full http redirect [R=302], not just an infile redir) from /search?q=some+value to /search/some+value quite easily, and then use a route to go from /search/(.*) to /search/query/$1 or whatever.

Seems a little complex "just" to get a neater URL out of it, tbf...
#3

[eluser]stuffradio[/eluser]
Code:
$route['search/(:any)'] = 'search/$1';
#4

[eluser]richthegeek[/eluser]
stuffradio - the problem isn't the internal routing, but going from a form to a slash-delimited URL, which can only be done with htaccess or javascript (or both, for graceful degradation)
#5

[eluser]dudel[/eluser]
Hello and thank you for your responses.
Do i need to create a htaccess file in the controller`s directory and add this line in it or add it to the default htacces file from the root?

How do I redirect from htaccess?

RewriteRule ^search/(.*)$ search/$1 [R=302]

I`m trying this and it doesn`t work.

Thank you, again.
#6

[eluser]Twisted1919[/eluser]
Code:
<form action="" method="post" id="theForm">
<input type="text" name="word" value="" id="word" />
</form>

// jquery javascript code
$('form#theForm').submit(function(){
var word = $('#word').val();
if(word.length < 3){
    return false;
}
_window._location.href = '&lt;?php echo rtrim(site_url(),'/');?&gt;/search/'+$('#word').serialize();

return false;
});
Remove the _ before window and location
#7

[eluser]richthegeek[/eluser]
Twisted1919: perfect except for the 5% (overzealous figure) of users who have JS disabled for some arcane reason. The apache rewrite is the fallback for this, and should work as this (not tested):

Code:
RewriteRule ^search/(\?[^=]*=(.*))?$ search/$2 [R=302,L]

Your form open tag would be like so in this case, to allow the fallback to work before being overwrriten

Code:
&lt;form action="&lt;?=site_url('search');?&gt;" method="get" id="theForm"&gt;
#8

[eluser]dudel[/eluser]
Yeah, I don`t really know why there are people out there with JS disabled.
Thanks for the informative replys. Enjoy your sunday Smile
#9

[eluser]Michael Wales[/eluser]
Code:
class Search extends Controller {

  function index($query = NULL) {
    if ($this->input->post('query') && $query === NULL) {
      redirect('search/' . $this->input->post('query'));
    }

    // The user is now at our super-search engine friendly search URL
    // Get SERPs and load view
  }

}
#10

[eluser]Cesar Kohl[/eluser]
I don't know if it's exactly what you need, but to search I made this controller:

Code:
function search($keyword=''){
        if($_POST){
            redirect('search/'.$this->input->post('search'));
        }else{
            //Script continues
            echo $keyword;
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB