Jump to content

Tell a Story with Variables


southfloridarentalz

Recommended Posts

I have a beginners script with html form that captures random words entered by users.

This form should be sent to the story.php which displays the full story using the variables entered by users.

 

Why isn't this working?

 

story.html

 

<html>

<head>

<title>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 cappucino">drinking cappucino</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>

 

storySimple.php

 

<html>

<head>

<title>Little Boy Who?</title>

</head>

<body>

<h1>Little Boy Who?</h1>

 

<h3>Values from the story page</h3>

 

<table border = 1>

<tr>

  <th>Variable</th>

  <th>Value</th>

</tr>

 

<tr>

  <th>color</th>

  <td><? print $color ?></td>

</tr>

 

<tr>

  <th>instrument</th>

  <td><? print $instrument ?></td>

</tr>

 

<tr>

  <th>anim1</th>

  <td><? print $anim1 ?></td>

</tr>

 

<tr>

  <th>anim2</th>

  <td><? print $anim2 ?></td>

</tr>

 

<tr>

  <th>anim3</th>

  <td><? print $anim3 ?></td>

</tr>

 

<tr>

  <th>place</th>

  <td><? print $place ?></td>

</tr>

 

<tr>

  <th>vegetable</th>

  <td><? print $vegetable ?></td>

</tr>

 

<tr>

  <th>structure</th>

  <td><? print $structure ?></td>

</tr>

 

<tr>

  <th>action</th>

  <td><? print $action ?></td>

</tr>

 

</table>

<form>

</html>

 

story.php

 

<html>

<head>

<title>Little Boy Who?</title>

</head>

<body>

<center>

 

<h1>Little Boy Who?</h1>

 

 

<?

 

print <<<HERE

<h3>

Little Boy $color, come blow your $instrument!<br>

The $anim1's in the $place, the $anim2's in the $vegetable.<br>

Where's the boy that looks after the $anim3?<br>

He's under the $structure, $action.

</h3>

HERE;

?>

 

</center>

 

</body>

</html>

 

PLEASE HELP!

 

 

Link to comment
Share on other sites

Use $_POST['color'] or $_POST['instrument'] to reference posted form inputs not $color or $instrument.

 

Also when printing user inputted data onto a page you need to clean it to stop XSS attacks, use strip_tags() and htmlentites() to remove any HTML (most importably <script> tags).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.