感谢有人向我解释为什么在尝试获取单选按钮组选择时为什么只能从PHP返回空字符串。
$currentlyemployed = null;
if (isset($currentlyemployedyes)) {
$currentlyemployed = "Yes";
}
else {
$currentlyemployed = "No";
}
注意:同一表单中的文本输入内容可以得到它们的值。
示例片段:
开始表格标记
<div class="row">
<div class="col-sm-6 text-left">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
<p class="help-block"></p>
</div>
</div>
</div>
</div>
END FORM MARKUP
启动PHP代码片段
$currentlyemployed = $_POST['currentlyemployed']; RETURNS empty string
/* FYI
$currentlyemployedyes = $_POST['currentlyemployedyes']; RETURNS "Yes"
$currentlyemployedno = $_POST['currentlyemployedno']; RETURNS "No"
*/
END PHP CODE SNIPPET
答案 0 :(得分:0)
您编写的代码可以正常工作。您可以查看下面的代码段。
引导与否无关紧要。 name="currentlyemployed"
从表单发送到php服务器,然后php将其绑定到$_POST
关联全局数组中。
<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
echo "Currently Employed: ".$_POST['currentlyemployed'];
?>
<hr>
<div class="row">
<div class="col-sm-6 text-left">
<form method="POST">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" />Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" />No</label>
<p class="help-block"></p>
</div>
</div>
<input type="submit">
</form>
</div>
</div>
答案 1 :(得分:0)
我认为这不是合法的“答案”,而是“解决方法”。
想提供出处,但忘了我在哪里发现的,甚至可能在堆栈溢出的地方。
<div class="row">
<div class="col-sm-6 text-left">
<h5>Are you currently employed?</h5>
<div class="control-group">
<div class="controls">
<label>
<input type="radio" id="currentlyemployedyes" name="currentlyemployed" value="yes" **onclick="$('#currentlyemployed').val('Yes');"** />
Yes</label>
<label>
<input type="radio" id="currentlyemployedno" name="currentlyemployed" value="no" **onclick="$('#currentlyemployed').val('No');"** />
No</label>
<p class="help-block"></p>
</div>
**<input name="currentlyemployed" id="currentlyemployed" type="hidden" value="" />**
</div>
</div>
</div>