Page 1 of 1
change URL when submitting form.
Posted: Mon May 18, 2009 6:32 pm
by tomsace
Hey,
Currently when I submit a search form on my site the URL is something like this: categories.php?search=keyword&id=search&sort=name&page=1... Which is kind of long and ugly, I have got it to work exactly the same but when entering: categories/keyword/search/name/1 which looks alot neater and easier to understand...
But now then, how do I make the submit button go to this new improved URL rather than the old ugly one?
Thanks for any help.
Tom.
Re: change URL when submitting form.
Posted: Mon May 18, 2009 6:37 pm
by Christopher
Are the search, id, sort, etc. parameters form fields? Is your form is using method="get" ?
Re: change URL when submitting form.
Posted: Mon May 18, 2009 6:42 pm
by tomsace
Yes and yes.
I use the parameters to organise the next page (e.g. sort=name, which sorts the page in order of name).
I just want the user to click submit and instead of the big url, I want the new smaller one.
Re: change URL when submitting form.
Posted: Mon May 18, 2009 7:19 pm
by Christopher
You will need to use javascript to build the action URL. You will probably need to do some tricks to not have the form fields sent -- maybe a separate form with no field or just set the document URL.
Why not just use method="post" ?
Re: change URL when submitting form.
Posted: Tue May 19, 2009 11:29 am
by tomsace
Hey,
If I used the javascript way to change the url, if a user has javascript turned off, wont they be able to search my site?
Is there a php way to do this?
This is my form (just in case):
Code: Select all
<form name="form" action="/categories.php" method="get">
Search: <input class="input" type="text" name="search" value="<?php echo"$search" ?>" />
<input type="hidden" name="id" value="search" />
<input type="hidden" name="sort" value="name" />
<input type="hidden" name="page" value="1" />
<a href="#" onclick="document.form.submit(); return false;"><img src="/images/btn_search.jpg" width="17" height="17" border="0"></a>
</form>
Re: change URL when submitting form.
Posted: Tue May 19, 2009 2:05 pm
by Benjamin
If they had javascript turned off it would use the old style urls. Why not use POST though?
Re: change URL when submitting form.
Posted: Tue May 19, 2009 2:48 pm
by tomsace
Oh thats good,
What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
Re: change URL when submitting form.
Posted: Tue May 19, 2009 5:19 pm
by Christopher
tomsace wrote:What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
Try it and see.
Re: change URL when submitting form.
Posted: Tue May 19, 2009 5:53 pm
by andycain
tomsace wrote:Oh thats good,
What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
If you use the GET method then all the information is submitted in the URL.
If you use the POST method then its not visible in the URL.
For example if I had the following form....
Code: Select all
<form action="myscipt.php" method="POST">
<input type="text" value="test" name="textfield">
<input class='submit' type="submit" name="submit" id="submit" value="Submit" />
</form>
I could then read the value of the text field and then output it in php with the following code:
Code: Select all
<?php
$data = $_POST('textfield');
echo $data;
?>
Now all you would see in the browser when the user clicks submit is
http://www.myurl.co.uk/myscript.php
Hope that makes sense.
Andy
Re: change URL when submitting form.
Posted: Tue May 19, 2009 7:02 pm
by tomsace
Hey,
Changed from 'get' to 'post' but my search results shown were wrong.
Will have to try the javascript way!
Re: change URL when submitting form.
Posted: Tue May 19, 2009 7:42 pm
by tomsace
I have found a solution!!
I have a hidden value in the form ($q=y) then I have:
Code: Select all
if ($q == "y")
{
header( "Location: /categories/search/etc..." ) ;
}