Codin2012 Posted January 9, 2017 Share Posted January 9, 2017 I have a form to submit a email address and password. I get an undefined index error and if I try to use isset to fix the undefined index error I get the following error Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in and I have tried to use the suggestions to solve the problem but than another error pops up. Here is my php code . All help greatly appreciated. if (isset($_POST['submit']=="Sign Up")) { if (!$_POST['email']) $error.="<br />Please enter your email"; else if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) $error.="<br />Please enter a valid email"; if (!$_POST['password']) $error.="<br />Please enter your password"; and my html <form class="marginTop" method="post"> <div class="form-group"> <label for="email">Email Address</label> <input type="email" name="email" class="form-control" placeholder="Your Email" value="<?php echo addslashes($_POST['email']); ?>" /> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" name="password" class="form-control" placeholder="Password" value="<?php echo addslashes($_POST['password']); ?>" /> </div> <input type="submit" name="submit" value="Sign Up" class="btn btn-success btn-lg marginTop"/> </form> Quote Link to comment https://forums.phpfreaks.com/topic/302899-can-not-use-isset-to-fix-undefined-index/ Share on other sites More sharing options...
Solution benanamen Posted January 9, 2017 Solution Share Posted January 9, 2017 (edited) Change if (isset($_POST['submit']=="Sign Up")) To if ($_SERVER['REQUEST_METHOD'] == 'POST') You are also trying to use variables in your form without checking if those variables exist. Edited January 9, 2017 by benanamen 1 Quote Link to comment https://forums.phpfreaks.com/topic/302899-can-not-use-isset-to-fix-undefined-index/#findComment-1541229 Share on other sites More sharing options...
Codin2012 Posted January 9, 2017 Author Share Posted January 9, 2017 Change if (isset($_POST['submit']=="Sign Up")) To if ($_SERVER['REQUEST_METHOD'] == 'POST') You are also trying to use variables in your form without checking if those variables exist. Thank for your answer it solved my problem. Quote Link to comment https://forums.phpfreaks.com/topic/302899-can-not-use-isset-to-fix-undefined-index/#findComment-1541233 Share on other sites More sharing options...
Jacques1 Posted January 9, 2017 Share Posted January 9, 2017 As to the original problem: The code makes no sense, because you can only check if a variable is set. You're trying to apply isset() to the logical expression $_POST["submit"] == "Sign Up", which cannot be interpreted in any meaningful way. What you probably meant to write is a combination of two checks: // is the submit parameter set, and does its value say "Sign Up"? if (isset($_POST['submit']) && $_POST['submit'] == 'Sign Up') { ... } However, this is very poor practice, because now your entire processing script relies on one stupid submit button and its exact label. Simply checking for the HTTP POST method as demonstrated by benanamen makes a lot more sense. Quote Link to comment https://forums.phpfreaks.com/topic/302899-can-not-use-isset-to-fix-undefined-index/#findComment-1541234 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.