In the course of learning PHP, I've been following this series on youtube: So far, its been really smooth sailing. I'm using VS Code 1.21.1, I just installed PHP 7, and am running XAMPP 7.2.3 (only running Apache so far), and viewing the output in Chrome, all in Windows 7. The code in the completed tutorial looks like this
<form action="index.php" method="GET">
Name:<br><input type="text" name="name"><br>
Age:<br><input type="text" name="age" size="5"><br><br>
<input type="submit" value="Submit">
</form>
<?php
$name = $_GET['name'];
$age = $_GET['age'];
if(isset($name) && isset($age) && !empty($name) && !empty($age)) {
echo 'I am '.$name.' and I am '.$age.' years of age.';
} else {
echo 'Please type something.';
}
?>
This never worked for me.
Tutorial 22 Undefined index screenshot
The if(isset) statement towards the end of the code is supposed to avoid the "undefined index" error, but never did for me. I ended up trying quite a few fixes, including adding in PHPCS linting, in case there was something new and exciting I was missing. The first 9 errors led me to discover an entire world of things the tutorial missed (and maybe most were just warnings... but still...)
My (partial?) solution was lifted from a different youtube video, where a name was given to the submit button and ONLY that field was tested against (in the tutorial, the test is also that the fields aren't blank. This solution does NOT check for that.) Note there is also a ton of other boilerplate added in trying to solve all of the linted issues, none of which solved the issue... but the question remains, why does THIS work to avoid the invalid index issues, but the one in the tutorial did not?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="index.php" method="GET" enctype="form-data/multipart">
Name:<br><input type="text" name="name"><br>
Age:<br><input type="text" name="age" size="5"><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
/**
* MyClass File Doc Comment
*
* PHP Version 7
*
* @category MyClass
* @package MyPackage
* @author A N Other <another@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
* @link http://www.holygenericboilerplatebatman.com/
*/
if (isset($_GET['submit'])) { // It's before the variables, so it works?
$name = $_GET['name'];
$age = $_GET['age'];
echo 'I am '.$name.' and I am '.$age.' years old.';
} else {
echo 'Please type something.';
}
?>
</body>
</html>
The only obvious difference i can see is having the if(isset) statement placed before the variables are declared, but using the one from the tutorial in that position made the page load properly, but then NOT render the entered name and age below the form like it's supposed to. The URL would update with the PHP entries, but the page would still say "Please type something" instead of switching to the other line of text with the variables filled in.
I don't understand the difference between the approaches enough to know what I'm supposed to be learning! :S
The other thing I noticed in all my reading/attempts is that 99% of all undefined index error suggestions are focused on the '$_POST' method, not '$_GET' including here on SO. As a result, I don't believe this is a duplicate question. Is this something else I'm missing that's obvious? Thanks in advance!