0

I have a URL in which a querystring is produced by a PHP script. Various values are displayed in the querystring.

Basically, I need to remove a specific value from the query string when a visitor clicks on a link or a 'remove' button.

So, the querystring looks like this:

http://www.foo.com/script.php?bar1=green&bar2=blue

But when a link or 'remove' button is clicked by a user, bar1=green is removed, and the visitor is directed to the following URL:

http://www.foo.com/script.php?bar2=blue

I thought this would be easy using basic HTML with a form or anchor but I haven't been able to do it so far.

Just so you know, i do not have access to the code on the PHP script itself; it is hosted remotely and is called to my webpage by a PHP wrapper using an iframe.

Any suggestions greatly appreciated.

Many thanks,

Matt

Matt
  • 345
  • 1
  • 6
  • 16
  • Based on the description, you will need to have access to the PHP code to do what you are trying to do. Do you have a an actual URL we can look at? It may help us better understand what you are looking for. – Chuck Burgess Nov 20 '10 at 16:22

1 Answers1

4

You can remove the value from the query string using this code:

<?php
function parseQueryString($url,$remove) {
    $infos=parse_url($url);
    $str=$infos["query"];
    $op = array();
    $pairs = explode("&", $str);
    foreach ($pairs as $pair) {
       list($k, $v) = array_map("urldecode", explode("=", $pair));
        $op[$k] = $v;
    }
    if(isset($op[$remove])){
        unset($op[$remove]);
    }

    return str_replace($str,http_build_query($op),$url);

} 
echo parseQueryString( "http://www.foo.com/script.php?bar1=green&bar2=blue","bar2");
?>
ynh
  • 2,919
  • 1
  • 19
  • 18
  • Thanks for the response. I tried this but I couldn't get it to work. I think a simple html form button would be more appropriate than PHP? – Matt Nov 20 '10 at 18:31
  • you need to have php5 on your server – ynh Nov 20 '10 at 18:33
  • How do I make this dynamic, in the sense that echo parseQueryString() equals the current query string in the URL? – Matt Nov 20 '10 at 18:59
  • echo parseQueryString("http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],"bar2"); – ynh Nov 20 '10 at 19:11
  • Brilliant, got this to work! Your code returns the query string minus the variable, so, surrounding your code by a html anchor returns the URL + querystring minus the variable. Thanks for your help! – Matt Nov 20 '10 at 19:33
  • I have posted a follow up question to this here. – Matt Nov 21 '10 at 13:35