0

嗨,我是一个初学者 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;
}
?>

我在这里做错了什么?

4

3 回答 3

1

试试这个,它的工作原理,你已经使用methods而不是method你的段落形式......而且你使用了错误的变量名submitin $_POST['']it should be 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;
           }

?>
于 2012-12-12T18:43:16.843 回答
0

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

您的创建按钮被命名为“创建”,因此将其添加到 $buttons 数组中,然后为“创建”编写一个处理程序,例如:

$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;

}

于 2012-12-12T18:25:06.827 回答
0

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

此外,您在这里还有另一个错字: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;
}
于 2012-12-12T18:14:53.617 回答