tags:

views:

1960

answers:

5

Using Codeigniter.

I have a search form on each of my pages. If I use form helper, it defaults to POST. I'd like the search term to show up in the URI: example -- mysite.com/search/KEYWORD

I've been on Google for about an hour to no avail -- only articles on how "GET" is basically disabled because of the native URI convention. But seriously, I can't be the first person to want this kind of functionality. Am i?

Much thanks.

A: 

I don't know much about CodeIgniter, but it's PHP, so shouldn't $_GET still be available to you? You could format your URL the same way Google does: mysite.com/search?q=KEYWORD and pull the data out with $_GET['q'].

Besides, a search form seems like a bad place to use POST; GET is bookmarkable and doesn't imply that something is changing server-side.

Justin Voss
No. "GET" is disabled in Codeigniter
jmccartie
but you can enable it
Thorpe Obazee
+2  A: 

As far as I know, there is no method of accomplishing this with a simple POST. However, you can access the form via Javascript and update the destination. For example:

<form id="myform" onsubmit="return changeurl();" method="POST">
<input id="keyword">
</form>

<script>
function changeurl()
{
    var form = document.getElementById("myform");
    var keyword = document.getElementById("keyword");

    form.action = "http://mysite.com/search/"+escape(keyword.value);

    return true;
}
</script>
64BitBob
issue with Codeigniter here -- apostrophe's in the search string will not be accepted by the CI URI library: "The URI you submitted has disallowed characters."
jmccartie
A: 

Check out this post on how to enable GET query strings together with segmented urls.

http://codeigniter.com/forums/viewthread/56389/#277621

After enabling that you can use the following method to retrieve the additional variables.

// url = http://example.com/search/?q=text
$this->input->get('q');

This is better because you don't need to change the permitted_uri_chars config setting. You may get "The URI you submitted has disallowed characters" error if you simply put anything the user enters in the URI.

muitocomplicado
+2  A: 

There's a better fix if you're dealing with people without JS enabled.

View:

<?php echo form_open('ad/pre_search');?>
   <input type="text" name="keyword" />
</form>

Controller

<?php
    function pre_search()
    {
        redirect('ad/search/.'$this->input->post('keyword'));
    }

    function search()
    {
        // do stuff;
    }
?>

I have used this a lot of times before.

Thorpe Obazee
Thorpe - how do you deal with apostrophe's?
jmccartie
You should modify this in the config file: $config['permitted_uri_chars'] = 'a-z 0-9~%.:_\-';to$config['permitted_uri_chars'] = '\'a-z 0-9~%.:_\-';
Thorpe Obazee
A: 

Here is the best solution:

$uri = $_SERVER['REQUEST_URI'];

$pieces = explode("/", $uri);

$uri_3 = $pieces[3];

Thanks erunways!

Jimmy