Years ago, when I was fiddling with PHP, I found out that I needed name attribute on html element even I have a form having id, action to url and post method. I have my html tags having ids. This was surprising as I was sure that it was enough.
For input controls to be posted back to the server from a form, I needed to assign each relevant input control a name, not an id. id is good for Javascript to locate elements in the DOM, though name can be used for the same which I don’t prefer so I used both of them if I needed to use PHP.
The ‘name’ is a must. It becomes the array index into the request array, $_GET or $_POST. This script illustrates the essential moving parts of an HTML form and a PHP action script.
Examine the following example:
";
// SHOW HOW TO DO FORM INPUT, VALIDATION, AND THANK YOU PAGE
// IF THERE IS SOMETHING THAT HAS BEEN POSTED
if (!empty($_POST)) {
// THIS IS THE VALIDATION PROCESS - ONLY TWO RESULTS
// EITHER A FAILURE PAGE OR A THANK YOU PAGE
// CHECK TO SEE IF IT IS WHAT WE EXPECT
if ($_POST["xyz"] != 'XYZ') {
// CREATE ERROR PAGE IF DATA FAILED VALIDATION
echo PHP_EOL . "SORRY YOU DID NOT ENTER XYZ";
} else {
// CREATE THANK YOU PAGE IF DATA PASSED VALIDATION
echo PHP_EOL . "THANK YOU, ";
die("ALL DONE NOW");
}
}
// END OF PHP - DROP INTO HTML TO PUT UP THE FORM
?>