Ricky55 Posted February 1, 2016 Share Posted February 1, 2016 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 Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted February 1, 2016 Solution Share Posted February 1, 2016 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'])) : ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 1, 2016 Share Posted February 1, 2016 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'; Quote Link to comment Share on other sites More sharing options...
Ricky55 Posted February 1, 2016 Author Share Posted February 1, 2016 (edited) 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 February 1, 2016 by Ricky55 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.