如何将表单(输入)和输出放在同一个文件 html/php 中


How to put the form (input) and the ouput in the same file html/php

我想通过使用与该文本位于同一文件中的表单来填充文本,该文本位于 pintStory() 函数中,我想要表单和程序是一个文件。

<! DOCTYPE html>
 <html>
 <head>
 <title>Story</title>
 <link rel="stylesheet" type="text/css href="story.css"/>
 </head>     
 <body>
 <?php
 storyForm();
 printStory();
 function storyForm(){
print <<<HERE
<h1>Story</h1>
<h3>Please fill in the blanks below,and then I'll tell you a story</h3>
<form method="post" action="">
 <fieldset>
   <label>Color:</label>
   <input type="text" name="instrument"/>
   <label>animal</label>
   <input type="text" name="anim1"/>
   <label>another animal</label>
   <input type = "text" name = "anim2" />
   <label>one more animal:</label>
   <input type = "text" name = "anim3" />
   <label>place: </label>
   <input type = "text" name = "place" />
   <label>vegetable: </label>
   <input type = "text" name = "vegetable" />
   <label>structure: </label>
   <input type = "text" name = "structure" />
   <label>action: </label>
    <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>
  <button type = "submit">
  Tell me the story
  </button>
</fieldset>
</form>
HERE;
 $color=filter_input(INPUT_POST, "color");
 $instrument=filter_input(INPUT_POST, "instrument");
 $anim1=filter_input(INPUT_POST, "anim1");
 $anim2=filter_input(INPUT_POST, "anim2");
 $anim3=filter_input(INPUT_POST,"anim3");
 $place=filter_input(INPUT_POST,"");
 $vegetable=filter_input(INPUT_POST, "vegetable");
 $structure=filter_input(INPUT_POST,"action");

 }
 function printStory(){
global $color;
global $instrument;
global $anim1;
global $anim2;
global $anim3;
global $place;
global $vagetable;
global $structure;
print<<<HERE
<h1>Little Boy who?</h1>
HERE;

 print<<<HERE
 <h2>
 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.
 </h2>
  HERE;
 }
 ?>


</body>
</html>

试试这个它会起作用:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

$_SERVER['PHP_SELF']可帮助您在同一页面上重定向。 并请更正表单方法的拼写。将pst替换为 post