JakeForDesign Posted February 28, 2014 Share Posted February 28, 2014 I originally had the error: "undefined index: password" I researched the error and figured out that I needed to be using isset() When should I use isset(every time I use the post array?) 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; } Quote Link to comment Share on other sites More sharing options...
Solution requinix Posted February 28, 2014 Solution Share Posted February 28, 2014 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; } Quote Link to comment Share on other sites More sharing options...
JakeForDesign Posted February 28, 2014 Author Share Posted February 28, 2014 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 28, 2014 Share Posted February 28, 2014 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. 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.