change URL when submitting form.
Moderator: General Moderators
change URL when submitting form.
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: change URL when submitting form.
Are the search, id, sort, etc. parameters form fields? Is your form is using method="get" ?
(#10850)
Re: change URL when submitting form.
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.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: change URL when submitting form.
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" ?
Why not just use method="post" ?
(#10850)
Re: change URL when submitting form.
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):
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>
Last edited by Benjamin on Tue May 19, 2009 2:05 pm, edited 1 time in total.
Reason: Changed code type from text to html.
Reason: Changed code type from text to html.
Re: change URL when submitting form.
If they had javascript turned off it would use the old style urls. Why not use POST though?
Re: change URL when submitting form.
Oh thats good,
What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: change URL when submitting form.
Try it and see.tomsace wrote:What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
(#10850)
Re: change URL when submitting form.
If you use the GET method then all the information is submitted in the URL.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 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>Code: Select all
<?php
$data = $_POST('textfield');
echo $data;
?>
Hope that makes sense.
Andy
Last edited by Benjamin on Tue May 19, 2009 8:32 pm, edited 1 time in total.
Reason: Changed code type from text to html, php.
Reason: Changed code type from text to html, php.
Re: change URL when submitting form.
Hey,
Changed from 'get' to 'post' but my search results shown were wrong.
Will have to try the javascript way!
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.
I have found a solution!!
I have a hidden value in the form ($q=y) then I have:
I have a hidden value in the form ($q=y) then I have:
Code: Select all
if ($q == "y")
{
header( "Location: /categories/search/etc..." ) ;
}
Last edited by Benjamin on Tue May 19, 2009 8:33 pm, edited 1 time in total.
Reason: Changed code type from text to php.
Reason: Changed code type from text to php.