Skip to:
Content
Pages
Categories
Search
Top
Bottom

Retrieve a variable from a GET

  • @city4run

    Participant

    Hello,
    I would like to retrieve a variable from a GET, e.g. /?variable=value

    I tried with this code:

    if (isset($_GET['var1'])) {
        echo "OK";
    }

    but it doesn’t return anything.
    Moreover if I print $_GET it return an empty array.

    What is the correct way to do it ?

Viewing 12 replies - 1 through 12 (of 12 total)
  • @shanebp

    Moderator

    if I print $_GET it return an empty array

    Then there is nothing in $_GET. You should see $_GET variables in the browser address

    if (isset($_GET['var1'])) 
        echo "OK";
    else
        echo "There is no variable called 'var1' being passed.";

    @city4run

    Participant

    Hello Shane,
    I called manually the URL in the browser with the ?var1=TEST to be sure that it’s called properly but nothing change.

    I’m calling it from the buddypress-xprofile-custom-fields-type plugin, do you think could be some filter that intercept querystring and reset it ?

    @shanebp

    Moderator

    I’m calling it from the buddypress-xprofile-custom-fields-type plugin, do you think could be some filter that intercept querystring and reset it ?

    I doubt it.
    Where in the plugin are you calling it?
    In a function?
    And what is that function hooked to?
    Is that hook being called on the page where you manually call the $_GET? Probably not.

    @city4run

    Participant

    Yes, I’m calling in to the public function edit_field_html
    I’m sure it’s fired because I write the else condition and it goes into it.

    The url is called with /index.php/members/userXX/profile/edit/?var1=aaaa

    @henrywright

    Moderator

    Hi @city4run

    It might help if we see all of your code?

    @city4run

    Participant

    Hello Henry, take a look here http://pastebin.com/LB0E98hB

    @city4run

    Participant

    Hi @henrywright, did you take a look to the code ?

    @city4run

    Participant

    However I notice this: when I put the following code into the functions.php file, it return OK.

    if ($_GET['var1']) {
    				echo "OK";
    			}

    When I call the page using the BPress EDIT page URL http://www.mysite.com/index.php/members/userxxx/profile/edit/?var1=TEST it seems to do a double page request because it says: Notice: Undefined index: var1 in functions.php

    I also tried to add an EXIT and it show me the OK

    if ($_GET['var1']) {
    				echo "OK";
    				exit();
    			}

    @henrywright

    Moderator

    using ?var1=test in the URL, what does the following display?

    if ( isset( $_GET['var1'] ) ) {
        echo $_GET['var1'];
    }

    @city4run

    Participant

    I put it into my code file and also into functions.php but always empty.

    I changed it to:

    if ( isset( $_GET['var1'] ) ) {
        echo $_GET['var1'];
        exit();
    }

    In my code file it return empty, into functions.php it return test

    @henrywright

    Moderator

    I put it into my code file and also into functions.php but always empty.

    I think this indicates there is something weird going on. My advice is to remove all custom code, disable all plugins and revert back to TwentyFifteen theme. Then try again. If it works then, we can be sure you have something on your install that’s conflicting. The task is then to find out what.

    @city4run

    Participant

    Really thanks Henry, I’ll do this and I’ll let you know…

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Retrieve a variable from a GET’ is closed to new replies.
Skip to toolbar