发送带有$ _POST的单选框值 [英] Send radio box value with $_POST
问题描述
即使字段为空,如何将单选或复选框值发送到$ _POST数组?
How can I send a radio or checkbox value to the $_POST array even when the field is empty?
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="gender">
<input type="submit">
</form>
如果我没有提交数据就点击提交",这就是我从页面中得到的信息.
Here is what I get from the page if I hit submit without even filling out data.
Array
(
[name] =>
[email] =>
)
如您所见,在不触摸输入类型文本的情况下,它们的值被推送到$ _POST数组中.如何使用收音机盒执行此操作?我本质上是想设置"它,尽管它与文本输入一样是空白的.
As you see, without touching the input type text, their value was pushed to the $_POST array. How can I do this with the radio box? I essentially want to "set" it, although it is blank just like the text inputs.
我知道我总是可以打电话
I know I could always call
<?php
if(isset($_POST['gender'])) {
//Code
}
?>
但这不一定是我想要的.我需要它自动设置.预先感谢!
But that's not necessarily what I'm looking for. I need it to set automatically. Thanks in advance!
推荐答案
尝试一下将起作用:
Unchecked radio
元素未提交,因为它们不被视为successful
.因此,您必须检查是否使用isset
或empty
函数发送了它们.
Unchecked radio
elements are not submitted as they are not considered as successful
. So you have to check if they are sent using the isset
or empty
function.
<?php
if(isset($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre>';
}
?>
<form action="test.php" method="POST">
<input type="text" name="name">
<input type="text" name="email">
<input type="radio" name="Gender" value="1"/>Male
<input type="submit" name="submit" value="submit"/>
</form>
这篇关于发送带有$ _POST的单选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!