phpget-method

Keeping GET variables from 1 page to anonter


The issue is that I want to keep the search variables that I used with a GET form when moving through the pages. It's like google does when you search for something and then you want to search for images, you just click on the images and don't need to replace the search. I did see that the link for their images or other kind of search already has the GET string in the url when the page is created. So is there a variable that contains everything from the url string and I can put it back in the href to the next page.

Example: my current url: localhost/index.php?name=John and I have a link to another page lets say edit.php It would be useful to put a code like echo 'edit.php?'.$GET_var.'; and this to echo to edit.php?name=John

EDIT: I will probably use $string = http_build_query($_GET); But another thing will be the get the part with index.php. So if I'm on this page(index.php) if the link to this page should be index.php and the other links to have the ?query arguments


Solution

  • you can try this

    // suppose your url is index.php?name=john&city=jordan
    
    $arr_temp = array();
    foreach($_GET as $key=>$val)
    {
       $arr_temp[] = $key."=".$val; // name=john or city=jordan into arr_temp array
    }
    
    $params = implode("&", $arr_temp); // implode with & name=john&city=jordan
    
    echo 'edit.php?'.$params; // combine params to new link
    

    UPDATE 2 :

    $params = $_SERVER['QUERY_STRING'];
    echo 'edit.php?'.$params;