PHP表单单选按钮
我所要做的就是以下面的编码样式生成一个带有文本输入字段和按钮的表单。文本输入字段工作正常。但是,单选按钮类不是。单选按钮类应该有一个组标题,PHP表单单选按钮,php,Php,我所要做的就是以下面的编码样式生成一个带有文本输入字段和按钮的表单。文本输入字段工作正常。但是,单选按钮类不是。单选按钮类应该有一个组标题,addOption函数应该用单选按钮显示每个选项。当我测试我的代码时,没有标题“Scoops Number of Scoops”,只有一个单选按钮表示“Three Scoops”,而不是“one Scoops”和“Two Scoops”。我做错了什么?这是我的密码: IceCreamForm.php: <?php require_once 'Form
addOption
函数应该用单选按钮显示每个选项。当我测试我的代码时,没有标题“Scoops Number of Scoops”,只有一个单选按钮表示“Three Scoops”,而不是“one Scoops”和“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();
<?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;
}
}
<?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/>";
}
}
<?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/>";
}
}
TextInput.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();
<?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;
}
}
<?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/>";
}
}
<?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/>";
}
}
因为您的单选按钮
构造函数没有设置标题。仅选择$和$类型
...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,
];
}
然后在生成HTML()
中
return join('', array_map(function($item) {
return sprintf('<label>%s</label> <input type="radio" value="%s">', $item['label'], $item['value']);
}, $this->options));
返回连接(“”,数组映射(函数($item)){
返回sprintf('%s',$item['label'],$item['value']);
},$this->options));
谢谢!我理解标题部分,并且能够解决这个问题。我不太理解addOption数组修复程序。这样的代码片段是什么样子的?例如,您自己的表单
类addInput()
方法。您还需要在generateHTML()
中迭代它们,因此,我有函数addOption(string$label,string$value){$this->label=$label;$this->value=$value;$this->options[]=$label;}
,但我会把foreach($this->options as$label){$this->radiobutton.=$label->generateHTML();}
在generatehtml()方法中?这就是迭代的样子吗?另外,您应该为单选按钮选项使用name
属性,以免单选按钮脱节。我建议让RadioButton
构造函数接受一个字符串作为要分配给$this->name
的名称,使实例变量TextInput::$name
受到保护(这样RadioButton就可以使用它),然后在RadioButton::generateHtml()
中的lambda函数中添加name=“%s”
的值为$this->name
的sprintf
的额外参数。是的,您应该使用name
属性。为了简洁起见,我删除了不必要的代码。我还建议使用类似laravel或symfony的框架,而不是自己编写。