在PHP中,我想检查是否选中了单选按钮;我用isset做这个,但有些东西不起作用。
HTML:
<form action="test.php" method="post" name="form_contribute" id="form_contribute">
<fieldset>
<legend>Required information</legend>
<p>Type of publication</p>
<div id="radio_buttons">
<label for="p_book">Book</label>
<input type="radio" name="p_radio" id="p_book" value="p_book">
<label for="p_article">Article</label>
<input type="radio" name="p_radio" id="p_article" value="p_article">
<label for="journal">Journal</label>
<input type="radio" name="p_radio" id="p_journal" value="p_journal">
</div>
<div id="contribute">
Title <input type="text" name="quote_title" id="quote_title">
Author(s)* <input type="text" name="author" id="author">
Title <input type="text" name="title" id="title">
ISBN <input type="text" name="isbn" id="isbn">
Publisher <input type="text" name="publisher" id="publisher">
Source <input type="text" name="source" id="source">
Addendum <textarea id="addendum"></textarea>
<div id="instructions">
<ul>
<li>Add only published non-fiction works; </li>
<li>Fill in all the required fields (designated by an asterix at the end);</li>
<li>Write the ISBN without space or special characters; if no ISBN, use identifier code as replacement;</li>
<li>Title should be written in English; exceptions are made for works unpublished in English;</li>
<li>Publisher should be the publisher of where your source derives from, not the original publisher of the work;</li>
<li>Source is to be used for verification; add chapter, section, page;</li>
<li>Use addendum to add other relevant info (e.g. if you've translated the material yourself; if the author is anonymous;) </li>
</ul>
</div> <!-- end #instructions -->
</div>
</fieldset>
<div class="big_button">
<input type="submit" name="edify_button" id="edify_button" value="Edify">
</div>
</form>
在php中:
的error_reporting(E_ALL); ini_set('display_errors','On');
if (isset($_POST['edify_button']) && isset($_POST['p_radio'])) {
$radio_values = $_POST['p_radio'];
if ($radio_values) {
echo 'checked';
}else {
echo 'unchecked';
}
}
我也试过!empty($_POST['my_radio']
。我没有任何错误,只是一个空白页面。在if语句中,在不同的测试中,我添加了if $radio_values == null
,然后添加了一些内容,但这也无济于事。在php中,是否有一个我不知道的值?
答案 0 :(得分:2)
使用双嵌套方法,例如:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if (isset($_POST['edify_button'])){
if(isset($_POST['p_radio'])) {
$radio_values = $_POST['p_radio'];
echo 'checked';
}else {
echo 'unchecked';
}
}
?>
它会让你的问题消失,然后工作; - )
答案 1 :(得分:0)
if (isset($_POST['my_button']) && isset($_POST['my_radio'])) {
$radio_values = $_POST['my_radio'];
if ($radio_values) {
echo 'not null';
}else {
echo 'null';
}}
如果未选择任何收音机,您将获得空白页面,因为您正在同时验证isset($_POST['my_button']) && isset($_POST['my_radio'])
,只有在选中任何单选按钮时,isset($_POST['my_radio']
才会返回true。