Jump to content

Prevent undefined get variable


Ricky55
Go to solution Solved by cyberRobot,

Recommended Posts

Hi Guys

 

PHP beginner here.

 

I'm passing a variable via a link just so I can conditionally load some javascript. Its all working fine but I'm getting an undefined variable error. 

 

On my link I have:


<form id="jtex" method="get" action="/folding-beds/">
   <input type="hidden" name="varname" value="jtex">
   <p><a href="javascript:{}" onclick="document.getElementById('jtex').submit(); return false;">View our range of J-Tex™ Folding Beds »</a></p>
</form>

 
Then on the page that this form links to I have
 
$isTtex = $_GET['varname'];
 
Then in my footer I'm using this code to conditionally load some javascript. 
 
<?php if (isset($isTtex)) : ?>
 
// Run some JS
 
<?php else : ?>
 
// Run some other JS
 
<?php endif; ?>
 
I've turned off error reporting for a temp fix but I really want to fix properly.
 
I've tried a few solutions I've found online but they seemed to prevent the variable being set at all.
 
Should also say that this isn't anything to do with showing private data or anything like that so security is not a concern for this one.
 
If you guys could shed any light I'd be very grateful.
 
Thanks in anticipation.
 
Ricky
 
 
Link to comment
Share on other sites

  • Solution

What is the exact error message? Does it refer to the following line:

$isTtex = $_GET['varname'];

If so, you could change it to

if(isset($_GET['varname'])) { $isTtex = $_GET['varname']; }

However, I wonder if the undefined error refers to the code in your footer. Perhaps the code is imported in a different scope from the rest of the PHP code. If that's the case, $isTtex below will be undefined:

<?php if (isset($isTtex)) : ?>

To fix that, you could use

<?php if (isset($_GET['varname'])) : ?>
Link to comment
Share on other sites

To add to cyberRobot's response. In addition to checking if $_GET['varname'exists, you would likely have a similar error when you try to reference $isTtex later in the code if it does not get defined, so I would suggest the following:

 

 

$isTtex = (isset($_GET['varname'])) ? $_GET['varname'] : 'some_default_value';
Link to comment
Share on other sites

Thanks guys. It turned out to be. 

 

Just wanted to also say how much I appreciate your answers. I've used this forum a few times and my questions have always been answered correctly and quickly.

 

Great job guys. 

if(isset($_GET['varname'])) { $isTtex = $_GET['varname']; }
Edited by Ricky55
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.