5

I have a site that uses a GET form to allow a user to specify a zipcode, and then provide sorted results by distance based on that entry. If no zipcode is present, then it simply displays all results.

My code works great. The problem is when I change from Page 1 to Page 2 I lose my query string.

For other technical reasons, I cannot use the POST method. Specifically I need to be able to save this query state somehow and check the url of the requested page, and the reappend that query string if it's empty.

Any thoughts on how to do this? The site uses a lot of jQuery, but I'm not sure if jQuery has a way to remember this string across multiple pages. The site uses PHP as well. I don't mind storing the results in a PHP session variable and then reconstructing the URL somehow, but I was hoping it would be a bit more straightforward than that. Thanks for your thoughts

Blue

user658182
  • 2,148
  • 5
  • 21
  • 36
  • Simply thinking, you could append `location.search` to all `` elements. – pimvdb Mar 16 '11 at 19:40
  • oh that sounds like a great idea - basically the script would loop through all the links on the page and just add that when the url was empty? It looks like this could possibly be my solution then? [link](http://stackoverflow.com/questions/486896/adding-a-parameter-to-the-url-with-javascript) <--- that link is not showing up. I don't know how to format it here to display correctly – user658182 Mar 16 '11 at 19:58
  • 2
    You should do this in server code when your paging URLs are generated--doing this in JS is inappropriate (unless the whole search operation is client-based) – Michael Haren Mar 16 '11 at 20:12

3 Answers3

2

You could try adding it to all <a href> elements, but then only <a> links will pass the query string to other pages. Anyway, you can do this:

$('a').each(function() {
    $(this).attr("href",
                 $(this).attr("href") + 
                 (location.search == '' ? '?' : '&') + 
                 location.search.substring(1));
});

The ampersand is to make sure that any query string data is separated from the new data, whilst the '?' is added if no query string exists at the moment. The substring(1) is used to remove the leading ? which already appears in location.search.

I just realised that this doesn't work if an <a> doesn't have a ? yet, but that shouldn't be too difficult to fix.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • @pimvdb this worked perfectly. With a little extra code to detect the ? and if an argument is already present, every link in my site is correctly constructed on page loads. Thanks so much! – user658182 Mar 19 '11 at 03:20
  • @user658182: That's great to hear. I don't want to be annoying, but if an answer has helped you, please tick it by clicking the tick on the left of the answer. Thank you :) – pimvdb Mar 19 '11 at 10:29
  • @pimvdb says I need 15 reputation :/ I will as soon as I gain the street creds, heh. – user658182 Mar 20 '11 at 17:48
  • @pimvdb I'm new to javascript. how can I check if window.location.search is set or empty? window.location.search == ''; doesn't seem to be working for me nor does window.location.search.value.length < 1; – user658182 Mar 21 '11 at 12:54
  • @user658182: For me, it does work correctly. On this page, no query string is available and `location.search == ''`. When you add a query string using `?blahblah`, then `location.serach == '?blahblah'`. As for `.length`, you shouldn't also use `.value`. Just do `location.search.length < 1` (or, just `== 0` as a length `< 1` basically is `== 0`). – pimvdb Mar 21 '11 at 13:00
  • @pimvdb I misunderstood how location.search works. When the original url is called it of course either does or does not have arguments. If it does, then the code you suggested works correctly, but the GET arguments are compounded exponentially each time you click a new url. What I need to do is: When the page loads, loop through the a.each as you suggested, check to see if that href value has a query string behind it. if so, ignore it. if not, append a '?argument' to it as @surreal suggested, from a session variable. "href" + works. Does this now become a reg ex issue? – user658182 Mar 23 '11 at 20:03
  • Oh well then it becomes a little more complex. Mootools More, however, consists of a query string parser. It might be worth looking there. – pimvdb Mar 23 '11 at 21:22
2

I think the best option for this kind of persisting data is to use a session. Your code checks for two things, a GET value and a session value. We'll assume if both are present, you take the GET value. This allows the user to submit a new one and override the old one.

session_start();
$zip = '';
if($_GET['zip'])
{
    //validate zip as needed
    $zip = $_GET['zip'];
    $_SESSION['zip'] = $zip;
}
elseif($_SESSION['zip'])
{
    $zip = $_SESSION['zip'];
}
else
{
    //handle default case
}

Then anytime your PHP code needs to reference the zip code for queries, etc, use the value in $zip. This way you don't rely on the page itself to deal with the zip values - you store it serverside and use it any time you need it.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

What is the code you use for pagination?

If I understand the problem correctly, you only need to add the search query and zipcode to the "Go to page 2" etc urls.

Simplified version:

<a href="results.php?page=2&<?php
    echo 'query=' . rawurlencode($_GET['query']);
    if (isset($_GET['zip'])) echo '&zip=' . rawurlencode($_GET['zip']);
?>">Next page</a>
Joel L
  • 3,031
  • 1
  • 20
  • 33