Jump to content

I feel like I'm going crazy ... assistance needed.


NoSalt
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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!");
}

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

@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 = '')

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

×
×
  • 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.