1

I have a simple search form:

<form method="get" action="">
<input id="search" placeholder="Search" type="search" name="search"></input>
</form>

I want to get the value from "search" and have it show up in the url like this:

http://domain.ca/blog/search/searchterm

So if a visitor searches for "kubrick", they'll get a URL like:

http://domain.ca/blog/search/kubrick

I can't use PHP. Is there a Javascript solution?

2
  • Is your PHP script allow these type of permalinks? Commented Dec 15, 2013 at 23:15
  • Yes, it does allow it. Commented Dec 15, 2013 at 23:34

2 Answers 2

7

You may try a javascript redirection for onsubmit event. To avoid empty searches, use the attribute "required" for the input.

Example:

<form onsubmit="window.location = 'http://domain.ca/blog/search/' + search.value; return false;">
<input id="search" placeholder="Search" type="search" name="search"></input>
<input type="submit" value="Send">
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

As soon as I click on that field it submits it, before I can input anything.
Sorry, wrong code. Changed the event to onsubmit and added the submit button.
0

Pedro did an awesome thing here, but including required input tag is also smooth,as it checks that a user is not making an empty search

1 Comment

Your answer should better be a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.