簡體   English   中英

PHP表單單選按鈕

[英]PHP Forms Radio Buttons

我要做的就是生成一個帶有文本輸入字段和下面編碼風格的按鈕的表單。 文本輸入字段工作正常。 但是,單選按鈕類不是。 單選按鈕類應具有該組的標題, addOption函數應使用單選按鈕顯示每個選項。 當我測試我的代碼時,沒有標題“Scoops”的標題,只有一個單選按鈕“Three Scoops”而不是“One Scoop”和“Two Scoops”。 我究竟做錯了什么? 這是我的代碼:

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

RadioButton.php

<?php

require_once 'TextInput.php';

class RadioButton extends TextInput
{
    const TYPE2 = "radio";
    private $selected = false;
    private $type;

    function __construct($selected = false, $type = self::TYPE2)
    {
        $this -> selected = $selected;
        $this -> type = $type;
    }

    function addOption(string $label, string $value)
    {
        $this -> label = $label;
        $this -> value = $value;
    }

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

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

        return "<label>$this->label</label><input id='$this->id' type='$this->type' value='$this->value' onNewLine='$this->onNewLine' $req/><br/>";
    }
}
...there's no title "Number of Scoops"...

因為您的RadioButton構造函數沒有設置標題。 只有$selected$type

...and only one radio button for "Three Scoops" and not for 
"One Scoop" and  "Two Scoops."...

因為addOption方法會覆蓋先前設置的標簽和值。 嘗試將它們添加到數組中。

function addOption(string $label, string $value) {
    $this->options[] = [
        'label' => $label,
        'value' => $value,
    ];
}

然后在你的generateHTML() ......

return join('', array_map(function($item) {
    return sprintf('<label>%s</label> <input type="radio" value="%s">', $item['label'], $item['value']);
}, $this->options));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM