ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Attempt to access $_GET variable causes PHP undefinied index error

April 15, 2011 4:48pm

Subscribe [4]
  • #1 / Apr 15, 2011 4:48pm

    I’m trying to send a variable through the URL and then utilize it in my EE template. I have a link on my site linking to:

    http://mydomain.com/quote?id=2

    and then in my quote template, I have the following code:

    <?php echo $_GET["id"]; ?>

    I’m receiving the following error: Notice: Undefined index: id when I go the above mentioned URL. After googling around, I found this link for $_GET/$_POST for EE 1 (http://expressionengine.com/legacy_docs/development/usage/input.html) and tried this:

    global $IN;
    echo $IN->GBL('id');

    but it still doesn’t work. Thanks in advance if anyone can help me out!

  • #2 / Apr 16, 2011 12:35pm

    giusi

    94 posts

    Hi.

    Maybe your variable is not properly set? You could try this to check for the variable existence:

    if (!isset($_GET["id"])) 
    {
    echo "variable is not set"; 
    }


    I would also give a look at the Session Variables Plugin. It’s a nice add-on from a reliable source that simplify the management of PHP sessions within ExpressionEngine.

    Bye

  • #3 / Apr 17, 2011 4:15pm

    Greg Salt

    3988 posts

    Hi Allison,

    If you are removing index.php using mod_rewrite in an .htaccess file then you may well have to make sure that the query string is passed through the rewrite properly. This is probably dependent upon your server environment but the documentation suggests that this may be the case.

    Cheers

    Greg

    @guisi, thanks for the help.

  • #4 / Apr 18, 2011 10:56am

    Thanks for the input guys! The plugin looks useful, however it appears to be mainly for session variables which I don’t think I need. I’m still having some trouble so maybe if I tell you the functionality I’m looking for it would be easier for you to help:

    I have a page with a form on my site. By default all the fields in the form are empty. However, if the user navigates to the form via a certain link, I’d like one field to be pre-populated with a value. My thought process was to have the link, instead of referencing just the form page (http://mydomain.com/form-page), reference the form page with a variable tacked onto the URL letting me know the user came from my link (ie http://mydomain.com/form-page?flag=1). Then use simple PHP to check whether or not the flag has been set, if so pre-populate the field.

    This is simple enough to do in a regular PHP site, however, I’m still struggling to achieve this functionality through EE because I’m still learning. Thanks in advance for any help!

  • #5 / Apr 18, 2011 11:41am

    giusi

    94 posts

    Couldn’t you store the string in a session variable then with a check use it if in page X or destroy it?

  • #6 / Apr 18, 2011 12:03pm

    I’m not sure exactly what you are suggesting. Are you saying to set a session variable on the page with my link to indicate I’ve just been on that page, and then check if the next page is the form page? If it is the form page, use the session variable to pre-populate my field, if not, destroy the session. Am I understanding that right?

  • #7 / Apr 18, 2011 1:19pm

    giusi

    94 posts

    Yes.

    It’a little bit of code you could put in a Snippet (if made with the Session Variables plugin) and calling in the templates.

  • #8 / Apr 19, 2011 5:53pm

    Sue Crocker

    26054 posts

    Thanks for the assist, giusi.
    Hi, Allison. Would that approach work for you?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases