使用 PHP 检索单选按钮的值


Retrieving the Value of a Radio Button using PHP

RadioButton的代码:

<h1 style="margin:0; margin-top:10px; padding:0; padding-left:25px; padding-bottom:10px; font-family:sans-serif;">
</h1>
<div style="background:#1794FF; color:#fafafa; padding:10px;">
<h3></h3>
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" />
<label for="radio3" class="css-label">Near Zipcode</label>
</td>
<td>
<input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" />
<label for="radio1" class="css-label">City-Wide</label>
</td>
</tr>
</table>
</div>
<div style="background:#1794FF; color:#222; padding:10px;">

菲律宾代码:

 <?PHP
 $selected_radio = $_POST['radiog_lite'];
  print $selected_radio;
   ?>

提交表单后,我到达上面的代码。但是,它说该值为"开"。为什么不打印所选的单选按钮名称?

你可以分配一个值:

PHP代码

 <form method="POST">
    <input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="1" />
    <label for="radio1" class="css-label">Neighbourhood Only</label><br>
    <input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" checked="checked" value="2"/>
    <label for="radio2" class="css-label">Zipcode Only</label><br>
    <input type="radio" name="radiog_lite" id="radio3" class="css-checkbox" value="3" />
    <label for="radio3" class="css-label">Near Zipcode</label><br>
    <input type="radio" name="radiog_lite" id="radio4" class="css-checkbox" value="4" />
    <label for="radio1" class="css-label">City-Wide</label><br>
    <input type="submit" value="Submit">
 </form>

PHP代码:

 <?php
  $selected_radio = $_POST['radiog_lite'];
  print $selected_radio;
 ?>

选按钮的默认值为"on",您应该在输入初始化中指定您的值,如下所示:

<input type="radio" name="radiog_lite" id="radio2" class="css-checkbox" value="Your Value Here" checked="checked"/><label for="radio2" class="css-label">Zipcode Only</label>

因此,您可以将"您的价值在这里"替换为"仅限邮政编码"。标签仅用于描述前端用户的单选按钮值。

在输入无线电上使用值属性。

使用表单方法"post",并为每个无线电输入值:

<form method="post">
<table>
<tr>
<td>
<input type="radio" name="radiog_lite" id="radio1" class="css-checkbox" value="Neighbourhood Only" />
<label for="radio1" class="css-label">Neighbourhood Only</label>
</td>
<tr>
</table>
<input type="submit" />
</form>