Nistor Alexandru
Nistor Alexandru

Reputation: 5393

Form generator in php

Hi I am a beginner PHP deveoloper and I am trying to create a form generator.I am having some problems with the generated form.When I click for example the "Paragraph" button a form is generated that contains a text field and another button.

I would like when I enter some text in the generated code and press the button to execute some function in my case just print the text.

This is my code:

<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;
}
?>

What am I doing wrong here?

Upvotes: 0

Views: 218

Answers (3)

Runcorn
Runcorn

Reputation: 5226

try this, it works, You have used methods instead of method in your paragraph form... and also you have used wrong variable name of submit in $_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;
           }

?>

Upvotes: 1

MAXIM
MAXIM

Reputation: 1221

I added a hiddent input here, this way you can enter the Paragraph condition to execute the code.

Also, you had another typo here: methodS="post" but method="post". It works now!

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

Upvotes: 0

jeremyj11
jeremyj11

Reputation: 704

If you want the title to echo you have to capture when the create button was clicked..

Your create button is named 'create' so add this to the $buttons array and then write a handler for 'create' eg:

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

}

Upvotes: 0

Related Questions