简体   繁体   中英

Form submit to get clean url

I don't know if I asked it right. I am a beginner. I am building a website where items can be searched. There is a form in the website and the value is searched using php get method. And the url after form submit is something like this.

example.com/?q=item

I modified the htaccess file so that I can use example.com/item/ to get the same result, Which works.

What I need to do is redirect to example.com/item/ after user submits the form . How can I achieve this? Should I use javascript or php or which is better?

Why not both. Javascript for users who have it enabled. PHP as a fallback for those that don't.

PHP

if (!empty($_GET['q']))
{
    $redirecturl = $domainname . "/search/" . $_GET['q'];
    $redirecturl = str_replace(" ", "-", $redirecturl);
    header('Location: ' . $redirecturl, true, 303);
    die();
}

Javascript

See: How to submit form using JavaScript and redirect to a URL?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM