PHP Request Variables

Posted on 2007-10-07
Last Modified: 2008-01-09

I've been using PHP for a few years properly now and there's one thing that's bugged me for a while and I need somebody to give me a hand.

Now, let's say I have a link like this: page.php?1=value1&2=value2

When I add a hyperlink to a page to change 2's value I'd do this: <a href="page.php?2=newvalue">Link</a>

Problem is, 1's value is now lost. So, I created a small script to output the request variables in the address to a string to use in my hyperlinks like this: <a href="page.php<?php echo $urlrequest ?>2=newvalue"></a>

The problem this time is that it will not change the current value of 2, it will just add it to the end like this:

What is the best way to overcome this issue so that I can change current request variables and possibly even remove specific request variables?

Question by:Phatzer
    LVL 49

    Accepted Solution

    You could use a function like so:

    $_GET = array('1' => 'Keep me', '2' => 'Change me', '3' => 'Delete me');

    $queryString = createQueryStringFromGet(array('2' => 'newvalue'), array('3')); // Change 2 parameter to its new value, and remove '3' parametter.

    echo $queryString;

    function createQueryStringFromGet($changedAndNewParameters=array(), $removeParameters=array()) {
      $newValues = $changedAndNewParameters + $_GET;
      foreach($removeParameters as $key) {

      $queryString = '';
      foreach($newValues as $key => $value) {
        $queryString .= empty($queryString) ? '?' : '&';
        $queryString .= $key .'='.urlencode($value);
      return $queryString;
    LVL 21

    Assisted Solution

    A very good idea would be to use session variables, this will overcome your problem of passing variables within different php pages and make variables accessible to all pages within a session.

    To do this:

    $_SESSION["2"] = $_REQUEST["2"];
    $_SESSION["1"] = $_REQUEST["1"];

    Then if you wanted to modify 2, simply:
    $_SESSSION["2"] = "newvalue";

    This way you will not have to pass along variables as arguments for every page, which saves a lot of trouble and much more elegant.


    As a side-note, for your line here:
    <a href="page.php<?php echo $urlrequest ?>2=newvalue"></a>

    do not forget the semi-colon at the end of the echo request (not too sure if this causes problems):
    <a href="page.php<?php echo $urlrequest; ?>2=newvalue"></a>
    LVL 10

    Author Comment

    Not tested the solutions yet, but I'm sure that it will be good enough for what I need to know. Using sessions for the info is a pretty good idea actually, in the past, I've only ever used sessions to handle logging in and basic user info.

    I'll split points evenly, thanks for the help, I'm sure I'll get round to using the idea soon :)

    @nizsmo: The semi-colon thing I know that it wasn't added, I just typed it all here, it's not a final piece of code just an idea that's all.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
    This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
    In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now