Issue on server with example from phpMySQL5 Chapter 3

Hi All

I’m learning PHP from PHP & MYSQL NOVICE TO NINJA BY KEVIN YANK, and am puzzled by the way one of the example scripts.

When I load the index.php, everything appears as it should. However, if I don’t enter any text and just hit the “GO” button, the page loads and displays “Welcome to our website, !”. Off course, there is no name, as nothing was entered, but shouldn’t reload index.php and include form.html.php, as no text was entered into the text boxes?

chapter3/welcome/form.html.php

<!DOCTYPE html> <html lang="en">
<head> <meta charset="utf-8"> <title>Form Example</title>
</head> <body>
<form action="" method="post"> <div><label for="firstname">First name:
<input type="text" name="firstname" id="firstname"></label> </div>
<div><label for="lastname">Last name: <input type="text" name="lastname" id="lastname"></label>
</div>
<div><input type="submit" value="GO"></div> </form>
</body> </html>

chapter3/welcome/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 {
}
include 'welcome.html.php';
}

chapter3/welcome/welcome.html.php

<!DOCTYPE html> <html lang="en">
<head> <meta charset="utf-8"> <title>Form Example</title>
</head> <body>
<p>
<?php echo $output; ?>
</p> </body>
</html>

Problem is that isset() on an empty string returns TRUE if the variable exists, so you need to use some other method to check whether anything was entered in the boxes. Also I assume that the first include outside of the braces is a thing in the post, not in your code, as it wouldn’t run that way.

You’d want something like


<?php
if (!isset($_REQUEST['firstname']))
  {
  include 'form.html.php';
  }
else
  {
  $firstName = $_REQUEST['firstname'];
  $lastName = $_REQUEST['lastname'];
  if (strlen($firstName)>0 && strlen($lastName)>0) {
    if ($firstName == 'Kevin' and $lastName == 'Yank') {
      $output = 'Welcome, oh glorious leader!';
      }
    else {
      }  // no need for this else here, unless you're planning on doing something, er, else
    include 'welcome.html.php';
    }
  else  // strlen() was not greater than zero for both entries, so show the form again.
    {
    include 'form.html.php';
    }
  }

Hi droop

Good spot on the “include”! Clearly I have copy and paste issues as well.

If isset is returning TRUE when the variable exists, doesn’t this then defeat the purpose of isset? I mean, how do you then test the an empty variable or for a NULL?

I can also see exactly what you’re saying with your example. I have since made changes to my code which appears to do away with the issues caused by isset. Are you able to advise if you can see any issue arising from it? It appears to test as I would have imagined isset to test.


<?php
if (!strlen($_REQUEST['firstname'])>0)
  {
  include 'form.html.php';
  }
  else
  {
  $firstName = $_REQUEST['firstname']; 
  $lastName = $_REQUEST['lastname'];
    if ($firstName == 'Kevin' and $lastName == 'Yank') 
    {
    $output = 'Welcome, oh glorious leader!';
    }
    else
    {
    include 'welcome.html.php';
}
 

As I read it, when the form posts it creates the variables as null strings - that is, they do contain a value, but it’s a null string as opposed to nothing. Kind of like having a string containing a character zero, which is not the same as a string containing nothing at all.

I think the idea of testing using isset() is that some functions can return errors if you attempt to use a variable that does not exist (as opposed to one that has no value), so you check first with isset() before attempting to use their value. I have little experience in the real world though, so perhaps others will answer that one.

Thanks for the update droop!

I also found this table which help to further illustrate what you were saying.