检查是否设置了单选按钮
Check if radio buttons are set
在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 中是否有我不知道的 checked 值?
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']
只有在选中任何单选按钮时才会 return 为真。
使用双层嵌套的方法,例如:
<?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';
}
}
?>
它会让你的问题消失,然后工作 ;-)
在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 中是否有我不知道的 checked 值?
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']
只有在选中任何单选按钮时才会 return 为真。
使用双层嵌套的方法,例如:
<?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';
}
}
?>
它会让你的问题消失,然后工作 ;-)