change URL when submitting form.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

change URL when submitting form.

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: change URL when submitting form.

Post by Christopher »

Are the search, id, sort, etc. parameters form fields? Is your form is using method="get" ?
(#10850)
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: change URL when submitting form.

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: change URL when submitting form.

Post 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" ?
(#10850)
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: change URL when submitting form.

Post 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>
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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: change URL when submitting form.

Post by Benjamin »

If they had javascript turned off it would use the old style urls. Why not use POST though?
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: change URL when submitting form.

Post by tomsace »

Oh thats good,
What would using 'post' do instead of using 'get'? Can I change the URL to what I wanted??
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: change URL when submitting form.

Post 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.
(#10850)
andycain
Forum Newbie
Posts: 19
Joined: Thu Jul 31, 2008 5:21 pm

Re: change URL when submitting form.

Post 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
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.
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: change URL when submitting form.

Post by tomsace »

Hey,
Changed from 'get' to 'post' but my search results shown were wrong.
Will have to try the javascript way!
tomsace
Forum Contributor
Posts: 167
Joined: Thu Jan 01, 2009 8:07 pm

Re: change URL when submitting form.

Post 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..." ) ;
  }
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.
Post Reply