简体   繁体   English

PHP表单单选按钮

[英]PHP Forms Radio Buttons

All I'm trying to do is generate a form with text input fields and buttons in the coding style below. 我要做的就是生成一个带有文本输入字段和下面编码风格的按钮的表单。 The text input fields are working fine. 文本输入字段工作正常。 However, the radio button class is not. 但是,单选按钮类不是。 The radio button class should have a title for the group and the addOption function should show each option with a radio button. 单选按钮类应具有该组的标题, addOption函数应使用单选按钮显示每个选项。 When I test my code, there's no title "Number of Scoops" and only one radio button for "Three Scoops" and not for "One Scoop" and "Two Scoops." 当我测试我的代码时,没有标题“Scoops”的标题,只有一个单选按钮“Three Scoops”而不是“One Scoop”和“Two Scoops”。 What am I doing wrong? 我究竟做错了什么? Here's my code: 这是我的代码:

IceCreamForm.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 : 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 : 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 : 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"...

Because your RadioButton constructor doesn't set a title. 因为您的RadioButton构造函数没有设置标题。 Only $selected and $type . 只有$selected$type

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

Because your addOption method overwrites the previously set label and value. 因为addOption方法会覆盖先前设置的标签和值。 Try adding them to an array. 尝试将它们添加到数组中。

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

Then in your generateHTML() ... 然后在你的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