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 !

2

1 Answer 1

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> 
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, it doesn't work. I add javascript and id="tftextinput" but it doesn't work
@ban I forgot to write e.preventDefault . Put it directly in front of the line window.location.href...
Can you post full code for me ? I cannot make it work
I've posted it above! @Ban

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.