My code for all three files is exactly as written in the book, however, after submission, the form outputs the ‘welcome.html.php’ page even if nothing is entered in the ‘firstname’ box of the form.
Is the problem that $_REQUEST[‘firstname’] is considered to have been set even though it’s value is nothing?
TIA
index.php
<?php
if (!isset($_REQUEST['firstname']))
{
include 'form.html.php';
}
else
{
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
if ($firstname == 'Kevin' and $lastname == 'Yank')
{
$output = 'Welcome, oh glorious leader!';
}
else
{
$output = 'Welcome to our web site, ' .
htmlspecialchars($firstname, ENT_QUOTES, 'utf-8') . ' ' .
htmlspecialchars($lastname, ENT_QUOTES, 'utf-8') . '!';
}
include 'welcome.html.php';
}
?>
To check the contents of $_REQUEST (although I would personally use $_POST since your form’s method is ‘post’), you can add a print_r($_REQUEST) to the index.php:
<?php
print_r($_REQUEST);
if (!isset($_REQUEST['firstname']))
{
include 'form.html.php';
}
else
{
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
if ($firstname == 'Kevin' and $lastname == 'Yank')
{
$output = 'Welcome, oh glorious leader!';
}
else
{
$output = 'Welcome to our web site, ' .
htmlspecialchars($firstname, ENT_QUOTES, 'utf-8') . ' ' .
htmlspecialchars($lastname, ENT_QUOTES, 'utf-8') . '!';
}
include 'welcome.html.php';
}
?>
It’ll show everytime what is contained in $_REQUEST, the first time you call index.php, and then after the submit of the form.
For the OP… I’m going thru this book as well, at a similar point also. I think you may be seeing some residual issues due to browser caching of values… try closing the browser and reloading the page from scratch.
I to have the same problem! =(
Can someone check why code does not work?
If submitting empty form the welcome page is displayed with "Welcome to our web site, "
The print_r shows that array is empty but I can not figure out why its not working…
Tried with different browsers and still same result…