Jump to content

Couple of isset() questions!(Beginner)


Go to solution Solved by requinix,

Recommended Posts

  • I originally had the error: "undefined index: password"
  • I researched the error and figured out that I needed to be using isset()
  1. When should I use isset(every time I use the post array?)
  2. Why does this prevent that error from showing up?

 

Also, is this how I should be coding it:

   

 
    // Check whether a password was entered and confirmed correctly
    if (isset($_POST["password"])=="") {
        $passwordIsEmpty = true;
    }
    if (isset($_POST["password2"])=="") {
        $password2IsEmpty = true;
    }
    if (isset($_POST["password"])!= isset($_POST["password2"])) {
        $passwordIsValid = false;
    } 
 
Instead of:
 
    if ($_POST['password'] == "") {
        $passwordIsEmpty = true;
    }
    if ($_POST['password2'] == "") {
        $password2IsEmpty = true;
    }
    if ($_POST['password'] != $_POST['password2']) {
        $passwordIsValid = false;
    }

 

Link to comment
https://forums.phpfreaks.com/topic/286596-couple-of-isset-questionsbeginner/
Share on other sites

  • Solution

isset() tells you if the field was submitted with the form. It doesn't give you the value, just that "yes or no" if it's there. Use isset() to check that the value is accessible. If so then you can get to it normally without those warnings.

 

if (!isset($_POST["password"]) || $_POST["password"] == "") { // not present OR empty
    $passwordIsEmpty = true;
}
if (!isset($_POST["password2"]) || $_POST["password2"] == "") { // not present OR empty
    $password2IsEmpty = true;
}

// here you'd have to do more isset()s, but the variables above already know about that
if (!$passwordIsEmpty && !$password2IsEmpty // only check if the passwords match if they were given at all
    && $_POST["password"] != $_POST["password2"]) {
    $passwordIsValid = false;
}

Wow thanks requinix. Yeah I knew I wasn't really using isset() correctly the way I was doing it above. I was following this netbeans wishlist tutorial and they were having me do it without isset at all! I'm glad you showed me the more logical way.

While you're learning, take a look at empty too. It's very similar to isset();

empty($var)  !isset($var) || $var == "" || $var === "0"
(I think that's precise)

 

Note that a "0" is considered empty; sometimes that's alright, but sometimes it isn't.

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.