内容简介:翻译自:https://stackoverflow.com/questions/4631224/getting-multiple-checkboxes-names-ids-with-php
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="checkbox" name="orange" id="orange"> <input type="checkbox" name="apple" id="apple"> <input type="checkbox" name="sky" id="sky"> <input type="checkbox" name="sea" id="sea"> <br> <br> <input type="submit" name="Submit" value="Submit"> </form>
仅当选中该复选框时,才会从表单提交复选框值.更重要的是,它是名称属性,而不是ID.
在PHP中有几种处理复选框的方法:
>为所有复选框指定相同的名称,后跟一对方括号,以便将整个集合视为数组.在这种情况下,请为每个复选框指定一个值.
>为每个复选框指定不同的名称和值.
>为每个复选框指定一个不同的名称,但没有值.
在每种情况下,您需要检查$_POST数组中是否存在复选框名称.
例如:
<input type="checkbox" name="color[]" id="orange" value="orange"> <input type="checkbox" name="color[]" id="apple" value="apple">
要获取这些复选框的值:
if (isset($_POST['color'])) {
$colors = $_POST['color'];
// $colors is an array of selected values
}
但是,如果每个复选框都有不同的名称和显式值,如下所示:
<input type="checkbox" name="orange" id="orange" value="orange"> <input type="checkbox" name="apple" id="apple" value="apple">
您仍然需要使用isset():
if (isset($_POST['orange'])) {
// orange has been set and its value is "orange"
}
如果未设置值,则默认值为“on”,但除非已选中,否则它将不在$_POST数组中,因此您仍需要使用isset().
翻译自:https://stackoverflow.com/questions/4631224/getting-multiple-checkboxes-names-ids-with-php
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 如何一条Mediainfo --Inform语句同时获取视频参数和音频参数多个Parameters
- 监控多个Kubernetes集群
- 管理多个远端仓库
- Git 推送到多个仓库
- 运行多个 npm script
- Python:尝试反序列化文件中的多个JSON对象,每个对象跨越多个但一致的间隔数量的行
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Building Social Web Applications
Gavin Bell / O'Reilly Media / 2009-10-1 / USD 34.99
Building a social web application that attracts and retains regular visitors, and gets them to interact, isn't easy to do. This book walks you through the tough questions you'll face if you're to crea......一起来看看 《Building Social Web Applications》 这本书的介绍吧!