2016-10-11 24 views
0

私がしようとしているのは、以下のコーディングスタイルのテキスト入力フィールドとボタンを含むフォームを生成することです。すべて正常に動作していますが、送信ボタンを生成する方法と、送信時にテキストフィールドのデータをデータベースに挿入する方法が混乱しています。下のコーディングスタイルを使用してサブミットボタンを正しく生成するにはどうすればよいですか?PHPフォーム送信ボタン/データベースに送信

IceCreamForm.php:

<?php 

require_once 'Form.php'; 
require_once 'RadioButton.php'; 
require_once 'TextInput.php'; 
require_once 'Validator.php'; 

$form = new Form(); 

$form -> addInput(new TextInput("First Name: ", "firstName")); 
$form -> addInput(new TextInput("Last Name: ", "lastName")); 
$form -> addInput(new TextInput("Ice Cream Flavor: ", "iceCreamFlavor")); 

$radio = new RadioButton("Number of Scoops: "); 
$radio -> addOption("One Scoop", "one"); 
$radio -> addOption("Two Scoops", "two"); 
$radio -> addOption("Three Scoops", "three"); 
$form -> addInput($radio); 

echo $form -> generateHTML(); 

Form.php:

<?php 

class Form 
{ 
    const GET = "GET"; 
    const POST = "POST"; 
    private $action = "someLocation.php"; 
    private $inputs = array(); 
    private $method = "POST"; 
    public $form = ""; 
    public $name = ""; 

    function __construct() 
    { 

    } 

    function addInput(TextInput $input) 
    { 
     $this -> inputs[] = $input; 
    } 

    function getAction(): string 
    { 
     return $this -> action; 
    } 

    function getMethod(): string 
    { 
     return $this -> method; 
    } 

    function setAction(string $action) 
    { 
     $this -> action = $action; 
     return $this; 
    } 

    function setMethod(string $method) 
    { 
     $this -> method = $method; 
     return $this; 
    } 

    function set($param, $value) 
    { 
     $this -> $param = $value; 
    } 

    function generateHTML(): string 
    { 
     $this -> form = '<form action="' . $this -> getAction() . '" 
     method="' . $this -> getMethod() . '">'; 
     foreach ($this -> inputs as $input) 
     { 
      $this -> form .= $input -> generateHTML(); 
     } 

     $this -> form .= '</form>'; 

     return $this -> form; 
    } 
} 

TextInput.php:

<?php 

    class TextInput 
    { 
     const TYPE = "text"; 
     private static $REQUIRED = "REQUIRED"; 
     private $defaultValue; 
     private $id; 
     private $label; 
     private $name; 
     private $onNewLine; 
     private $required = false; 
     private $type; 

     function __construct($label = "", $name = "", $defaultValue = "", $id = "", $onNewLine = "", $required = false, $type = self::TYPE) 
     { 
      $this -> defaultValue = $defaultValue; 
      $this -> id = $id; 
      $this -> label = $label; 
      $this -> name = $name; 
      $this -> onNewLine = $onNewLine; 
      $this -> required = $required; 
      $this -> type = $type; 
     } 

     public function generateHTML():string 
     { 
      $req = ""; 

      if ($this -> required == true) 
      { 
       $req = self::$REQUIRED; 
      } 

      return "<label>$this->label</label><input id='$this->id' type='$this->type' name='$this->name' onNewLine='$this->onNewLine' $req/><br/>"; 
     } 
    } 
+0

としてフォームのアクションやメソッドを追加するだけでなく、送信ボタンで別の入力を、追加される場合があります。 –

答えて

0

あなたは、変更の下に行う必要があります。

変更generateHTML()私TextInputクラスのthodにdefaultValueを追加します。

public function generateHTML():string 
{ 
    $req = ""; 

    if ($this -> required == true) 
    { 
     $req = self::$REQUIRED; 
    } 

    return "<label>$this->label</label><input id='$this->id' type='$this->type' name='$this->name' onNewLine='$this->onNewLine' value='$this -> defaultValue' $req/><br/>"; 
} 

以下のように送信ボタンを作成します。

$form -> addInput(new TextInput("", "submit_btn","Submit Button Name","submit_btn","",false,"submit")); 

データを保存するためには、

echo $form -> setAction("You post url where you want to save data"); 
echo $form -> setMethod("POST"); 
echo $form -> generateHTML();