2

I have this search bar:

<div id="tfheader">
    <form id="tfnewsearch" method="get" action="http://mywebsite.com">
            <input type="text" class="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
    </form>
<div class="tfclear"></div>
</div>

It worked great but when I search "keyword", it will redirect to http://mywebsite.com/?q=keyword.

How to redirect to http://mywebsite.com/keyword ? Thank you very much !

baao
  • 71,625
  • 17
  • 143
  • 203
Ban
  • 131
  • 1
  • 2
  • 9
  • possible duplicate of [.htaccess rewrite query string as path](http://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path) – Terry Dec 20 '14 at 12:53
  • You need to hook into form submit event: http://stackoverflow.com/questions/27409950/how-to-change-form-url-parameter-on-submit/27410081#27410081 After you have done that, you also need to make sure your server can understand this request. – dfsq Dec 20 '14 at 13:02

1 Answers1

5

You can do it with javascript

<script>
var a = document.getElementById('tfnewsearch');
a.addEventListener('submit',function(e) {
e.preventDefault();
var b = document.getElementById('tftextinput').value;
window.location.href = 'http://mywebsite.com/'+b;

});

</script>

and give your input field an ID called tftextinput.

<input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120">

I don't really understand why you would like to do this, as it is way more difficult to handle this request server side as it would be to simply handle the $_GET data you will receive when doing a standard form transmission.

EDIT:

Here is the full code:

<div id="tfheader">
    <form id="tfnewsearch" method="get" action="http://www.mywebsite.com">
        <input type="text" class="tftextinput" id="tftextinput" name="q" size="21" maxlength="120"><input type="submit" value="search" class="tfbutton">
    </form>
<div class="tfclear"></div>
</div>

<script>
    var a = document.getElementById('tfnewsearch');
    a.addEventListener('submit',function(e) {
        e.preventDefault();
        var b = document.getElementById('tftextinput').value;
        window.location.href = 'http://mywebsite.com/'+b;

    });

</script>
baao
  • 71,625
  • 17
  • 143
  • 203