Irfan Mir
Irfan Mir

Reputation: 2175

Change URL for when this form is processed

I have a form that has no action attribute, a method of get, and a single input field of type text with a name of q.

When one enters 'query' and submits the form, the URL of the confirmation page looks like http://mySite.com/search.php?q=query.

How can I get the URL to be http://mySite.com/search?q=query

And for multi-word searches (like for 'query one') http://mySite.com/search?q=query+one ?

All while still navigating to the confirmation / results page.

How can I do this? I would prefer a solution not involving .htacess

Upvotes: 0

Views: 106

Answers (4)

FrontEnd Expert
FrontEnd Expert

Reputation: 5803

Best way to do this is Rewrite htaccess file.. But you are telling u tried this

so this is pure php

In $a i have taken this string.. You have to get URL directly from the current page using PHP

   $a= 'http://mySite.com/search.php?q=query';
   echo $a;

   echo "</br>";
   $b= str_replace('.php','',$a);
   echo $b;

for space

   $a= 'http://mySite.com/search.php?q=query one';


   echo "</br>";
   $b= str_replace('.php','',$a);
   $c= str_replace(' ','+',$b);
   echo $c;

Not an efficient solution but it will work..

Upvotes: 3

zavg
zavg

Reputation: 11061

One of the possible ways to solve this problem is to route your requests to one PHP-file (index.php) which script parses request's URL and choose appropriate action (in your case - search).

If you have Apache Web server, you can use Rewrite Rules in htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Upvotes: 1

DAG
DAG

Reputation: 6994

For multiple terms simply do this:
$query = str_replace(' ', '+', $_GET['search']);

For the redirect you could check if $_GET is_empty() and then display another html-page accordingly:
if (!is_empty($_GET)) echo file_get_contents('result.html');
else echo file_get_contents('form.html')

A very basic way of doing it. You may need to add a few more checks though.

Upvotes: -1

Manish Jangir
Manish Jangir

Reputation: 505

Write the following code in your htaccess file

RewriteEngine On
RewriteRule ^search\?q\=([^/]*)$ /search.php?q=$1 [L]

and then use http://mySite.com/search as action file in your form

Upvotes: 1

Related Questions