我试图使用php获取所选单选按钮的值。但是,当我尝试回显收音机的值时,无论我在提交表单时选择了哪个单选按钮,它都会返回'on'。
HTML:
<article>
<form id="quiz" method="post" action="question3.php">
<fieldset id="question">
<legend>Question 2</legend>
<p>Where was CoffeeScripts first stable build announced? <br />
<label for="github">GitHub</label>
<input type="radio" name="ans2" id="github" value="GitHub"/> <br />
<label for="hackernews">Hacker News</label>
<input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br />
<label for="coffeescript">CoffeeScript.com</label>
<input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br />
<label for="dropbox">DropBox</label>
<input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br />
</fieldset>
<p><input type="submit" value="Submit Answer" id="subans" /></p>
</form>
</article>
然后在流程页面上:
<?php
if (isset($_POST['ans2'])) {
$ans = $_POST['ans2'];
echo "<p>$ans</p>";
}
else {
echo "Nothing was selected.";
}
?>
就像我说的那样,这只是向页面输出“on”。
帮助表示赞赏!
答案 0 :(得分:0)
你可以尝试这个吗,在PHP块中添加了isset($ _ POST ['subans'])
<article>
<form id="quiz" method="post" action="question3.php">
<fieldset id="question">
<legend>Question 2</legend>
<p>Where was CoffeeScripts first stable build announced? <br />
<label for="github">GitHub</label>
<input type="radio" name="ans2" id="github" value="GitHub"/> <br />
<label for="hackernews">Hacker News</label>
<input type="radio" name="ans2" id="hackernews" value="Hacker News"/> <br />
<label for="coffeescript">CoffeeScript.com</label>
<input type="radio" name="ans2" id="coffeescript" value="CoffeeScript"/> <br />
<label for="dropbox">DropBox</label>
<input type="radio" name="ans2" id="dropbox" value="Dropbox"/> <br />
</fieldset>
<p><input type="submit" value="Submit Answer" id="subans" name="subans" /></p>
</form>
</article>
在php页面/部分中,添加了isset($ _ POST ['subans'])。
<?php
if(isset($_POST['subans'])){ // added the submit button action check
if (isset($_POST['ans2'])) {
$ans = $_POST['ans2'];
echo "<p>$ans</p>";
}
else {
echo "Nothing was selected.";
}
}
?>