Been following tutorial, is this the best way and have I doen it correctly

Hi guys

Ive been following a tutorial and have come up with this.

The HTML is this :


<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>The story</title>
</head>

<body>
<h1>Story</h1>
<h3>Please fill in the blanks below, and I'll tell
    you a story</h3>
<form method = "post"
      action = "story.php">

<table border = 1>
<tr>
  <th>Color:</th>
  <th>
    <input type = "text"
           name = "color"
           value = "">
  </th>
</tr>

<tr>
  <th>Musical Instrument</th>
  <th>
    <input type = "text"
           name = "instrument"
           value = "">
  </th>
</tr>

<tr>
  <th>Animal</th>
  <th>
    <input type = "text"
           name = "anim1"
           value = "">
  </th>
</tr>

<tr>
  <th>Another animal</th>
  <th>
    <input type = "text"
           name = "anim2"
           value = "">
  </th>
</tr>

<tr>
  <th>Yet another animal!</th>
  <th>
    <input type = "text"
           name = "anim3"
           value = "">
  </th>
</tr>

<tr>
  <th>Place</th>
  <th>
    <input type = "text"
           name = "place"
           value = "">
  </th>
</tr>

<tr>
  <th>Vegetable</th>
  <th>
    <input type = "text"
           name = "vegetable"
           value = "">
  </th>
</tr>

<tr>
  <th>A structure</th>
  <th>
    <input type = "text"
           name = "structure"
           value = "">
  </th>
</tr>

<tr>
  <th>An action</th>
  <th>
    <select name = "action">
      <option value = "fast asleep">fast asleep</option>
      <option value = "drinking cappuccino">drinking cappuccino</option>
      <option value = "wandering around aimlessly">wandering around aimlessly</option>
      <option value = "doing nothing in particular">doing nothing in particular</option>
    </select>
  </th>
</tr>

<tr>
  <td colspan = 2>
    <center>
      <input type = "submit"
             value = "tell me the story">
    </center>
  </td>
</tr>
</table>

</form>
</body>
</html>

Ok now the php page to go with it.


<?php

$color = $_REQUEST["color"];
$instrument =$_REQUEST ["instrument"];
$anim1 = $_REQUEST ["anim1"];
$place = $_REQUEST ["place"];
$anim2 = $_REQUEST ["anim2"];
$vegetable = $_REQUEST ["vegetable"];
$anim3 = $_REQUEST ["anim3"];
$structure = $_REQUEST ["structure"];
$action = $_REQUEST ["action"];

echo <<<HERE
<h3>
Today the sky is $color, the people on picnic play their $instrument!<br>
The $anim1's is under the $place, the $anim2's love to eat  $vegetable.<br>
Where's the boy that looks after the $anim3?<br>
He's under the $structure, $action.
</h3>
HERE;
?>

What Im confused about is, $place, $anim2 and $amin3 are different colors in my code editor which signifies something is different about them but I cant figure out what…?

Also I tried to run the program without declaring the variables hoping that the php page would write the variables using the form inputs names but that didnt work.

Am I doing this correctly…?

I must add that both pages have been saved as .php, and the php program page has no declaration at the top of it… are these settings ok…?

PS The story makes no sense so dont worry about that

Don’t use $_REQUEST. You’re using method POST in your form, so use $_POST.
The result of the PHP page should be a valid HTML document, so in a real application you would need to add a declaration, and all the rest that is needed for a valid HTML document (html, head, title, body, ecc).

ahhh ok thanks man, I ve now put a declaration on the page aswell.

Also does that mean if I use ‘get’ in my form, the variables should also use ‘get’…? If my form used ‘request’, would that work with ‘request’ in my variables also…?

Is there a better way to do what Im doing or is this ok…

For some reason the php variables I mentioned, although it all works, are still a different color to the rest…

Request will accept either Post or Get, but is considered bad coding form. You should specify either Post or Get.

The colouring is just the editors interpretation of the code - it won’t always be 100% what you’re expecting (or even what it probably should be).

The code you’ve used looks fine to me, assuming it works as you’re expecting of course :wink:

The <center> tag is deprecated, so remove that. Use CSS text-align: center; instead.
Otherwise looks okay.

You should use [fphp]htmlentities[/fphp] on the user submitted values before displaying them.

Thanks guys.

Im looking into the html entities as I write this and to be honest had never heard of it until just now so really helpful :slight_smile:

The form is just about doing what I wanted but if only I could get my brain to work the same way…

Thanks again guys :slight_smile: