根据所选的单选按钮添加隐藏的输入类型

时间:2011-09-08 13:50:14

标签: php html

是否可以有条件地将隐藏的输入字段添加到表单中?

例如,我有一个PHP表单,正在向表添加值,如果appleID = 1或2,那么我想要1添加到我的表的水果列,如果appleID = 3我想要1添加到糖果列的我的桌子。我想我可能会做类似下面的事情,但无论我选择什么,它都会添加所有隐藏的值。或者我应该以不同的方式处理这个问题?

<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />

<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />

<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />

谢谢我没有用PHP做很多事情所以我需要进一步探索这个选项。感谢您的反馈。 我也在看下面的东西。但PHP听起来更好。 change field value when select radio buttons

2 个答案:

答案 0 :(得分:1)

您可以使用单选按钮(1,1,0 - 2,1,0 - 3,0,1)中的所有值,并在PHP脚本中接收它们后拆分它们,或者通过以下方式添加/删除隐藏的字段的JavaScript。

拆分示例:

HTML:

<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />

PHP:

if (!empty($_POST['appleID']))
{
    list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}

答案 1 :(得分:0)

最好将该逻辑放入PHP脚本而不是使用Javascript - 因为您无论如何都必须进行验证。

相关问题