showing undefined index

why is my code showing "Notice: Undefined index: name in F:\xampp\htdocs\aaa\cart\eg.php on line 15" here is the code <!DOCTYPE html> <html> <head> <title></title> </head> <body> <form action="eg.php" method="post"> Name:<input type="text" name="name"><br><br> Age:<input type="text" name="age"><br><br> <input type="submit" name="submit" value="submit"> </form> <?php echo $_POST['name']; ?> </html>

31st Jul 2017, 2:16 PM
james z phiri
james z phiri - avatar
1 Answer
+ 2
I added: <script> document .querySelector("#myForm") .action = "http://www.hashemian.com"+ "/tools/form-post-tester.php"; </script> and your form POSTs fine so the problem seems to be the code falling through to evaluate POSTvars when none have been sent (first run, typically). Best practices (as you learn them) might involve checking if the variable exists (it's been set), is of the expected type, is in the expected range, isn't too long/short (etc) before using it. Here you can just test if it's there: isset() and possibly: empty()
31st Jul 2017, 3:52 PM
Kirk Schafer
Kirk Schafer - avatar