php中的表单生成器

时间:2012-12-12 18:11:40

标签: php

您好我是初学PHP开发者,我正在尝试创建一个表单生成器。我对生成的表单有一些问题。当我点击例如“段落”按钮时,会生成一个包含文本字段的表单,另一个按钮。

我希望当我在生成的代码中输入一些文本并按下按钮执行某些功能时,只需打印文本。

这是我的代码:

<form action="" method="post">
   <button type="submit" name ="Paragraph">Paragraph Text</button>
   <button type="submit" name ="MultipleChoice">Multiple Choice</button>
   <button type="submit" name ="Name">Name</button>
   <button type="submit" name ="Adress">Address</button>
   <button type="submit" name ="Number">Number</button>
   <button type="submit" name ="CheckBoxes">Checkboxes</button>
   <button type="submit" name ="DropDown">Drop Down</button>
   <button type="submit" name ="Phone">Phone</button>
   <button type="submit" name ="Email">Email</button>
</form>

<?php 
   require_once 'addform.php';
   $buttons = array('Paragraph', 'MultipleChoice', 'Name', 
              'Adress', 'Number', 'CheckBoxes', 'DropDown', 'Phone', 'Email');
   foreach ($buttons as $button){
     if(isset($_POST[$button])){
        $title;
        $input;
        if($button == 'Paragraph'){
           ?>
           <form action="" methods="post">
             Paragraph title: <input type="text" name="title"/>
             <input type="submit" value="Create" name="create"/>
           </form>
           <?php
           if(isset($_POST['title'])){
             $title = $_POST['title'];
             echo $title;
           }
        }
     }
     break;
}
?>

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

试试这个,它有效,你在段落形式中使用了methods而不是method ...... 并且您在submit中使用了错误的$_POST['']变量名,它应该是isset($_POST['create'])

 <form action="" method="post">
   <button type="submit" name ="Paragraph">Paragraph Text</button>
   <button type="submit" name ="MultipleChoice">Multiple Choice</button>
   <button type="submit" name ="Name">Name</button>
   <button type="submit" name ="Adress">Address</button>
   <button type="submit" name ="Number">Number</button>
   <button type="submit" name ="CheckBoxes">Checkboxes</button>
   <button type="submit" name ="DropDown">Drop Down</button>
   <button type="submit" name ="Phone">Phone</button>
   <button type="submit" name ="Email">Email</button>
</form>

<?php 

   $buttons = array('Paragraph', 'MultipleChoice', 'Name', 
              'Adress', 'Number', 'CheckBoxes', 'DropDown', 'Phone', 'Email');
   foreach ($buttons as $button){
     if(isset($_POST[$button])){
        $title;
        $input;
        if($button == 'Paragraph'){
           ?>
           <form action="" method="post">
             Paragraph title: <input type="text" name="title"/>
             <input type="submit" value="Create" name="create"/>
           </form>
           <?php

        }
     }
     break;
}
     if(isset($_POST['create'])){

             $title = $_POST['title'];
             echo $title;
           }

?>

答案 1 :(得分:0)

我在这里添加了一个hiddent输入,这样你就可以输入Paragraph条件来执行代码了。

此外,您还有另一个拼写错误: methodS =“post” method =“post”。 它现在有效!

<form action="" method="post">
   <button type="submit" name ="Paragraph">Paragraph Text</button>
   <button type="submit" name ="MultipleChoice">Multiple Choice</button>
   <button type="submit" name ="Name">Name</button>
   <button type="submit" name ="Adress">Address</button>
   <button type="submit" name ="Number">Number</button>
   <button type="submit" name ="CheckBoxes">Checkboxes</button>
   <button type="submit" name ="DropDown">Drop Down</button>
   <button type="submit" name ="Phone">Phone</button>
   <button type="submit" name ="Email">Email</button>
</form>

<?php 
   require_once 'addform.php';
   $buttons = array('Paragraph', 'MultipleChoice', 'Name', 
          'Adress', 'Number', 'CheckBoxes', 'DropDown', 'Phone', 'Email');
foreach ($buttons as $button){
 if(isset($_POST[$button])){
    $title;
    $input;
    if($button == 'Paragraph'){
       ?>
       <form action="" methods="post">
         Paragraph title: <input type="text" name="title"/>
         <input type="hidden" value="set" name="Paragraph"/>
         <input type="submit" value="Create" name="create"/>
       </form>
       <?php
       if(isset($_POST['title'])){
         $title = $_POST['title'];
         echo $title;
       }
    }
 }
 break;
}

答案 2 :(得分:0)

如果您希望标题回显,则必须在单击创建按钮时捕获..

您的创建按钮名为'create',因此将其添加到$ buttons数组,然后为'create'编写处理程序,例如:

$buttons = array('create', 'Paragraph', 'MultipleChoice', 'Name', 
          'Adress', 'Number', 'CheckBoxes', 'DropDown', 'Phone', 'Email');
foreach ($buttons as $button){
 if(isset($_POST[$button])){
    $title;
    $input;
    if($button == 'Paragraph'){
       ?>
       <form action="" methods="post">
         Paragraph title: <input type="text" name="title"/>
         <input type="submit" value="Create" name="create"/>
       </form>
       <?php
    }elseif($button=='create' && isset($_POST['title'])){
       $title = $_POST['title'];
       echo $title;
    }
 }
 break;

}