mgp2008
mgp2008

Reputation: 3

Simple search that redirects to /user/"searchterm"/

Currently I'm working with:

<form action="/user/" method="get">
<input type="text" name="" id="" size="20" value="">
<input type="submit" value="Search">
</form>

But, this redirects to /user/?=searchterm
I would like it to redirect to /user/searchterm
I know this is easy I just cannot figure it out.

EDIT: If I was unclear. I would like the form to use the parameter typed in and redirect the browser to /user/(parameter)

Upvotes: 0

Views: 150

Answers (4)

Kneel-Before-ZOD
Kneel-Before-ZOD

Reputation: 4221

Changing '/user/?=searchterm' to '/user/searchterm' is hard to do; changing '/user/q=searchterm' to '/user/searchterm' is easier using either .htaccess file or mod-rewrite.
Querystrings should always be in the format '?q=a'; having it in the format '?=a' doesn't make much sense.

Upvotes: 0

PeeHaa
PeeHaa

Reputation: 72672

You would have two options to do this.

  1. Redirect the user after for submit to the correct page (example using PHP):

    HTML

    <form action="/usersearch/" method="post">
      <input type="text" name="user" id="" size="20" value="">
      <input type="submit" value="Search">
    </form>
    

    PHP

    if (!isset($_REQUEST['user'])) {
        header('Location: http://example.com/search'); //redirect back to search page
        exit();
    }
    
    header('Location: http://example.com/user/' . $_REQUEST['user']);
    exit();
    
  2. Use javascript to capture the submit and redirect (I'm using jQuery because I'm lazy ATM but it's alo possible with vanilla JS):

    ​(function($) {
        $('form').submit(function() {
            window.location = "http://example.com/user/" + $('input[name="user"]', this).val();
    
            return false;
        });
    })(jQuery)​
    

Upvotes: 2

Madara&#39;s Ghost
Madara&#39;s Ghost

Reputation: 174957

It can't (and shouldn't!) be done in regular HTML. You'll need to use JavaScript to fake the form submission (by redirecting to the link according to the search term, and using URL rewriting to point to the correct $_GET variable'ed address).

Upvotes: 2

Chillie
Chillie

Reputation: 1026

Change This:

<form action="/user/" method="get">

To This:

<form action="/user/" method="post">

By using get, the values are added to the URL. By using post the values are not added to the URL.

EDIT: Misread question...

I'm not sure why you want to do that, It will make the link act kinda like a page link, but anyway, I would suggest using a mod_rewrite if you are using Apache. There is another question on here with a good answer that might help. You would just need to have the value

https://stackoverflow.com/a/4674388/1076164

Upvotes: 0

Related Questions