Simple search: passing a form variable to a URI using CodeIgniter

I have a search form on each of my pages. If I use a form helper, by default it is equal $_POST. I would like the search query to display in the URI:

http://example.com/search/KEYWORD

I was at Google for about an hour, but to no avail. I just found articles on how it $_GETwas mostly disabled due to the native URI convention. I can't be the first to want this functionality, right? Thanks in advance!

+3
source share
5 answers

There is a better fix if you are 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 many times before.

+5
source

, POST. Javascript . :

<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>
+3

, GET URL-.

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

.

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

, allowed_uri_chars. "URI, , ", -, URI.

0

:

$uri = $_SERVER['REQUEST_URI'];

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

$uri_3 = $pieces[3];

erunways!

0

I don’t know much about CodeIgniter, but it is PHP, so it should not $_GETbe accessible to you? You can format your URL in the same way as on Google: mysite.com/search?q=KEYWORDand display the data with $_GET['q'].

Also, the search form seems like a bad place to use POST; GET is bookmarkable and does not mean that something is changing on the server side.

-2
source

Source: https://habr.com/ru/post/1698600/


All Articles