Build Your Own Database Driven Website chapter 3

Hello,

I am currently working on Chapter 3 in the book “Build Your Own Database Driven Web Site Using PHP & MySQL”. The problem comes in the last exercise. I have created a folder welcome, and putted “form.html.php” , “index.php” and “welcome.html.php” in it. I have copy/paste the code exactly like in the book.

When I view http://localhost:8888/welcome/ (yes my port is 8888), the HTTP ERROR 500 appears. However, when I view http://localhost:8888/welcome/form.html.php, the forms appear and I enter some text but when I click “GO” it reload “form.html.php”. When I load the welcome.html.php , the page is blank.

I think there is 2 sorts of problem here. One with the folder, and one with the code…

Here is the code:

form.html.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">    
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">    
  <head>    
    <title>Form Example</title>    
    <meta http-equiv="content-type"    
        content="text/html; charset=utf-8"/>    
  </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>

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';5    
}    
?>

welcome.html.php

    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">    
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">    
  <head>    
    <title>Form Example</title>    
    <meta http-equiv="content-type"    
        content="text/html; charset=utf-8"/>    
  </head>    
  <body>    
    <p>    
      <?php echo $output; ?>    
    </p>    
  </body>    
</html>```

Thank you

Hi francoiscarrier welcome to the forum.

The “HTTP ERROR 500” suggests there is a server configuration mistake somewhere. In particular, look for something like
DirectoryIndex index.html index.php
to make sure “index.php” is a default when no file is specified in the HTTP request.

The reason the form is reloading from the form page is because
<form action="" method="post">
An empty action value basically means “submit to this same file”. But the code that deals with the submitted input is not in that file.

Try going to
http://localhost:8888/welcome/index.php

Also, the “5” looks to be a typo
include 'welcome.html.php';5

When developing php I always have these lines at the beginning of the first php file.

<?php 
// PHP7 Specific for each file
declare ( strict_types=1 );

define('LOCALHOST', 'localhost' === $_SERVER[['SERVER_NAME'); 

// SET ONLINE DEFAULTS
   error_reporting(-1);
   ini_set('display_errors', '0');  // should save errors and warnings to a log file

if( LOCALHOST ):
  ini_set('display_errors', '1');
endf;

When errors or warnings appear, copy, paste and Google for a solution.

Thank you a lot Mittineague ! It was the typo mistake that did it ! Everything works fine now

1 Like

And thank you John_Betong for the advice !

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.