NoSalt Posted February 24, 2011 Share Posted February 24, 2011 Hello All I am not an expert PHP developer, but I thought I had a pretty good handle on the language. That is until I was goofing off yesterday, playing around with isset. All of a sudden, I feel like I have lost my mind. I will get straight to the point. Why does this display the message "THE VARIABLE WAS FOUND!" when no input is submitted? <html> <head> <title> form test </title> </head> <body> <div> <form action="" method="GET"> <table> <tr><td>enter myVar: </td><td><input type="text" name="myVar" id="myVar"></td></tr> <tr><td><button type="submit">submit</button></td><td><button type="reset">reset</button></td></tr> </table> </form> <?php if(isset($_GET['myVar'])){ print("]" . $_GET['myVar'] . "[<br>"); print("THE VARIABLE WAS FOUND!"); } ?> </div> </body> </html> If you were to click the submit button without putting anything into the myVar field, the message will still show up, but it shouldn't. If you submit an empty field there is nothing sent to the "next page", so to speak. If nothing is sent, then the isset should evaluate to false because there is no myVar to "GET", therefore the message should not be displayed. But, as you can see, the message is displayed whether or not you enter anything into the field. I have many sites that use isset that appear to be working as expected, so you can understand why this is driving me crazy. Would any of you nice people please explain to me why this is "working" when it shouldn't? Thanks for reading, and have a nice day. Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/ Share on other sites More sharing options...
samoht Posted February 24, 2011 Share Posted February 24, 2011 try putting in an action <form action="index.php?myVar="Var" method="GET"> <table> <tr><td>enter myVar: </td><td><input type="text" name="Var" id="Var" value=" " /></td></tr> <tr><td><button type="submit">submit</button></td><td><button type="reset">reset</button></td></tr> </table> </form> Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179091 Share on other sites More sharing options...
Skylight_lady Posted February 24, 2011 Share Posted February 24, 2011 Your form is definately going to display the message anyway. Even if the form is empty. You are using the code below, which mean's that it can have no value and display whats coming after it which is your message "THE VARIABLE WAS FOUND!" if(isset($_GET['myVar'])){ Assign this to your input box: <input type="text" name="myVar" id="myVar" value=""> and then you can use: if($_GET['myVar'] == "") { print ("No values added"); } else if ($_GET['myVar'] != "") { print("]" . $_GET['myVar'] . "[<br>"); print("THE VARIABLE WAS FOUND!"); } Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179096 Share on other sites More sharing options...
NoSalt Posted February 24, 2011 Author Share Posted February 24, 2011 But ... I thought if you attempted to GET or POST something that wasn't there, then isset would always evaluate to false. Is this not correct? When you look at the address bar, you will see "index.php?myVar=". How can isset evaluate to true if there is nothing to GET??? Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179097 Share on other sites More sharing options...
TOA Posted February 24, 2011 Share Posted February 24, 2011 http://php.net/manual/en/function.isset.php first example Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179099 Share on other sites More sharing options...
Skylight_lady Posted February 24, 2011 Share Posted February 24, 2011 No ..... it will always evaluate to true. Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179109 Share on other sites More sharing options...
samoht Posted February 24, 2011 Share Posted February 24, 2011 look at your url when you hit submit - myVar is there! even though blank, the var is set. Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179110 Share on other sites More sharing options...
TOA Posted February 24, 2011 Share Posted February 24, 2011 look at your url when you hit submit - myVar is there! even though blank, the var is set. And we circle back to example one in the manual pages. Try empty() Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179121 Share on other sites More sharing options...
NoSalt Posted February 24, 2011 Author Share Posted February 24, 2011 So, what you're telling me is that just the existence of the myVar variable means that it is "set", even if it is empty? Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179123 Share on other sites More sharing options...
samoht Posted February 24, 2011 Share Posted February 24, 2011 @DevilsAdvocate, You are right, but I think he was thinking that since he never set the variable himself (i.e. $myVar= ''; ) then he thought that is wasn't being set. Skylight_lady tried to explain that he was setting the variable simply by naming it and then submitting the form - but he perhaps did not understand how $_GET works? Anyway, if he just looks at the url after submission then he would see that myVar is in the url and therefore is set (even though it = '') Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179125 Share on other sites More sharing options...
TOA Posted February 24, 2011 Share Posted February 24, 2011 @DevilsAdvocate, You are right, but I think he was thinking that since he never set the variable himself (i.e. $myVar= ''; ) then he thought that is wasn't being set. Skylight_lady tried to explain that he was setting the variable simply by naming it and then submitting the form - but he perhaps did not understand how $_GET works? Anyway, if he just looks at the url after submission then he would see that myVar is in the url and therefore is set (even though it = '') I know what he meant, and I was agreeing with you sorry to give an impression otherwise. Wasn't trying to be snotty. My apologies Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179129 Share on other sites More sharing options...
TOA Posted February 24, 2011 Share Posted February 24, 2011 So, what you're telling me is that just the existence of the myVar variable means that it is "set", even if it is empty? You are correct Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179133 Share on other sites More sharing options...
samoht Posted February 24, 2011 Share Posted February 24, 2011 Wasn't trying to be snotty. My apologies I didn't think you were - I just was clarifing why NoSalt didn't seem to understand your help. He didn't think it applied (even though it did). Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179136 Share on other sites More sharing options...
NoSalt Posted February 24, 2011 Author Share Posted February 24, 2011 So, now that I have a handle on that, what does everybody recommend to check if a variable has a value associated to it in a GET or POST? Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179149 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 So, now that I have a handle on that, what does everybody recommend to check if a variable has a value associated to it in a GET or POST? Pseudocode: if (isset(/* var */) && !empty(/* var */)) { // do stuff } Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179153 Share on other sites More sharing options...
kenrbnsn Posted February 24, 2011 Share Posted February 24, 2011 I don't recommend using empty which can give a false positive if the value of the field is a zero (0). I check to see if the length of the variable is greater than 0: <?php if (isset($_POST['somevar']) && strlen($_POST['somevar']) > 0) { // // do stuff // } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179164 Share on other sites More sharing options...
NoSalt Posted February 24, 2011 Author Share Posted February 24, 2011 Very cool, thank you. And, thank you to all who took the time to read and reply. Now I can sit back and wait for my hair to grow back in. Quote Link to comment https://forums.phpfreaks.com/topic/228701-i-feel-like-im-going-crazy-assistance-needed/#findComment-1179172 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.