Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Arrays (?) and URLs

Posted by Caitlinn (Caitlinn), 9 September 2004
Okay, this will be a dozy for me to explain because -- whilst I can make this work -- I don't know the proper terminology.  So please bear with me ...

I have a form which contains a dropdown box (list of categories) and 51 checkboxes (one for each US state, plus DC).  The object of the form is to search the database for all companies that match the selected category and state(s).

The checkboxes on the form are written as:

Code:
<input type="checkbox" name="state_id[]" value="1" />Alabama


The information is then passed to the processing script via $_GET (I tried the same thing with $_POST, but the script kept losing the set variables as you paged through results).  I have no problem selecting information; it works like a charm.  I start running into problems with pagination and the $_GET results.  I suppose it's not really a problem, but more of an untidy look to the URL as result of the passed $_GET variables.

The URL looks like:
Code:
http://www.foobar.com/search_results.php?category=Contractor&state_id%5B%5D=1&state_id%5B%5D=2&state_id%5B%5D=3


on the first page and on the second, like:
Code:
http://www.foobar.com/search_results.php?state_id[]=1&state_id[]=2&state_id[]=3&category=Contractor&page=2&show=5


Surely there must be a neater URL scheme than this.  Any pointers in the right direction, please?  

Posted by admin (Graham Ellis), 9 September 2004
Hmmm ... I'm half with this / half reading this during a course.  I think you're maintaining state with your GET method via the URL and when you use the POST method it's getting lost, as the data isn't in the URL any longer.  I would be tempted to use a session but that's a whole new can o'worms.

Please post a follow up if you would like me to have a further look overnight tonight - quite happy to do so once the course is over.  But I have to teach Perl Objects in 10 minutes

Posted by Caitlinn (Caitlinn), 9 September 2004
Yes, please Graham, any further help with this would be much appreciated.  In the meantime, I will take your advice and look at the sesssions method.

Thanks a bunch.  

Later edit
I should also mention that pagination works off of $_GET too.  I've been fiddling with registering state_id and category variables as $_SESSION variables, but haven't had much luck so far; still losing the state_id and category variables as I paginate through the pages.

I did something like:
Code:
session_start();
 header("Cache-control: private"); //IE 6 Fix  
 
 $_SESSION['category'] = $_POST['category'];
     $_SESSION['state_id'] = $_POST['state_id'];


Then used the $SESSION['foo'] throughout the rest of my code, as needed.

Posted by admin (Graham Ellis), 10 September 2004
OK - A fresh morning.  Let's see if I've got this ....

You've filling in a form and getting results back and it's working like a charm ... but you need to move on to another set of results based on the original input, thus you need to carry on your original inputs through to the second result set.

The "conventional" way to do this is to add an extra query string onto the end of the URL and mimic the GET method of form submission - it sounds like it's more or less what you're doing already and it works fine, but it does give what we call the "Yuk" factor of horrid URLs.   Don't be too worried about this; you'll see exactly the same sort of thing happen if you use the search engines and go on to subsequent result pages (and you'll also see it at the top of pages on a forum like this one!)

The first alternative way of passing information on is via a POST method. But this means that you have to supply a form - at the least a submit button with hidden fields - rather than a simple "a href" with the data encoded, and it means that your users can't bookmark that second and subsequent page either.

The second alternative is to use a cookie so that your visitor can be remembered.  This has great attractions if you user is likely to be going through a whole series of pages, or is likely to be coming back and carrying on in an hour or two, or a day or two.   The PHP built in session functions will work like a charm and save you the need to write all your own cookie handling and cart code.

I *hope* I'm talking in the right direction here - I started off saying a "fresh" new morning but I'm not entirely fresh yet, even  though it's a new morning!

Posted by Caitlinn (Caitlinn), 10 September 2004
on 09/10/04 at 05:00:40, Graham Ellis wrote:
You've filling in a form and getting results back and it's working like a charm ... but you need to move on to another set of results based on the original input, thus you need to carry on your original inputs through to the second result set.


Yes, that's right.

Quote:
The "conventional" way to do this is to add an extra query string onto the end of the URL and mimic the GET method of form submission - it sounds like it's more or less what you're doing already and it works fine, but it does give what we call the "Yuk" factor of horrid URLs.   Don't be too worried about this; you'll see exactly the same sort of thing happen if you use the search engines and go on to subsequent result pages (and you'll also see it at the top of pages on a forum like this one!)


Query strings, on the whole, don't really bother me that much.  The Yuck factor came into play with the host of square brackets (the more states you have, the more square brackets you get -- and conceivably, someone could search all 50 states).  It's the first time I'd seen them used in query strings, so I was a little worried that I might be doing something a bit 'rigged'.

Quote:
The first alternative way of passing information on is via a POST method. But this means that you have to supply a form - at the least a submit button with hidden fields - rather than a simple "a href" with the data encoded, and it means that your users can't bookmark that second and subsequent page either.


Okay, this intrigues me because I'm not worried about bookmarking.  If using hidden fields on the form, how do you get the $state_id array to explode in the hidden field, so that when it is passed to the next page, it's ready to use?  And if you are using the POST method, don't you run the risk of losing your variables as you page through results, unless those variables are in the URL or registered as a part of a session?

Quote:
The second alternative is to use a cookie so that your visitor can be remembered.  This has great attractions if you user is likely to be going through a whole series of pages, or is likely to be coming back and carrying on in an hour or two, or a day or two.   The PHP built in session functions will work like a charm and save you the need to write all your own cookie handling and cart code.


I imagine I could take another look at sessions -- I probably did something wrong with my first effort.  But here's a question: can you use data supplied in GET in combination with data supplied in SESSION variables?

Thanks again, Graham.

Posted by admin (Graham Ellis), 11 September 2004
on 09/10/04 at 09:10:18, Caitlinn wrote:
If using hidden fields on the form, how do you get the $state_id array to explode in the hidden field, so that when it is passed to the next page, it's ready to use?  And if you are using the POST method, don't you run the risk of losing your variables as you page through results, unless those variables are in the URL or registered as a part of a session?


The "principle" is that you can carry information from one page to the next via the URL ("get"), or via hidden fields (via post or get methods) or via a cookie.   Then you have a secondary choice in that you can include all the information your want in the get/post/cookie or you can include just a unique key value which is the name of a disc file in a temporary area or a record in a database which in turn contains the information you want to pass on.

Sound a bit complicated?  It isn't - but - it can be a bit long winded to write / understand the first time, and you also need to have a mechanism to go back and clean up all the abandoned sessions unless you want you disc to start getting cluttered ... which is why the session functions and $_SESSION were introduced into PHP.

Quote:
 But here's a question: can you use data supplied in GET in combination with data supplied in SESSION variables?


Yes - both are available to you and if you want to retain something, simply copy it from $_GET to $_SESSION.

There's an example of a 4 stage process using sessions on our website at http://www.wellho.net/demo/sc6.php4 and you can see the source code of this from the training module description page at http://www.wellho.net/resources/H115.html. Basically, this is a great framework for any multipage application  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho