Jump to content

Taking an HTML form and a PHP program and making it into one working file.


boom_roasted

Recommended Posts

Story.html

<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<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 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>

 

Story.php

<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<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>

 

I want this to be in one file that asks for the variables and tells the story. You know, combining the form and program into one file. How do I do this?

Pending any syntax errors I may have created here is one way to do it:

 

<?php
if (isset($_POST['submit'])) {
$title = "Little Boy Who?";	
$output = <<<HERE
<h3>
Little Boy {$_POST['color']}, come blow your {$_POST['instrument']}!<br>
The {$_POST['anim1']}'s in the {$_POST['place']}, the {$_POST['anim2']}'s in the {$_POST['vegetable']}.<br>
Where's the boy that looks after the {$_POST['anim3']}?<br>
He's under the {$_POST['structure']}, {$_POST['action']}.
</h3>
HERE;
}else {
$title = "Story";
$output = <<<HERE
<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" name="submit"
       value = "tell me the story">
</center>
</td>
</tr>
</table>

</form>
HERE;
}
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<html>
<head>
       <title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title;?></h1>
<?php echo $output; ?>
</body>
</html>

 

 

EDIT: Fixed the post variables to be correctly accesed.

Pending any syntax errors I may have created here is one way to do it:

 

<?php
if (isset($_POST['submit'])) {
$title = "Little Boy Who?";	
$output = <<<HERE
<h3>
Little Boy {$_POST['color']}, come blow your {$_POST['instrument']}!<br>
The {$_POST['anim1']}'s in the {$_POST['place']}, the {$_POST['anim2']}'s in the {$_POST['vegetable']}.<br>
Where's the boy that looks after the {$_POST['anim3']}?<br>
He's under the {$_POST['structure']}, {$_POST['action']}.
</h3>
HERE;
}else {
$title = "Story";
$output = <<<HERE
<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" name="submit"
       value = "tell me the story">
</center>
</td>
</tr>
</table>

</form>
HERE;
}
?>
<!doctype html public "-//W3C//DTD HTML 4.0 //EN"> 
<html>
<head>
       <title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $title;?></h1>
<?php echo $output; ?>
</body>
</html>

 

 

EDIT: Fixed the post variables to be correctly accesed.

 

Thanks for the help. When I run that, I don't get any errors to start with, but when I click to submit it, it leads me to a "page cannot be found" page on internet explorer. Any idea why this is happening?

The issue is the action of the form - that script checks in itself if the form has been submitted and returns the story if it has. therefore change

 


<form method = "post"
          action = "Story.php">

 

to:

 


<form method = "post"
          action = "">

 

This will reload the same page, and now that isset($_POST['submit'])) returns true (since the form has been submitted) it will display the story back to the user.

 

EDIT: beaten to the punch - I'll leave it up as it may help explain a little better :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.