Checkbox的值获得与普通的input获得方法是不一样了,我们下面来看一篇PHP处理Checkbox多选框表单元素的例子,希望能够让各位知道如何使用php来接受checkbox的值哦。
最近做Form表单提交的时候,复选框Checkbox提交的数据不知道怎么处理,Checkbox允许多个选项,我们不可能每个选项都自定义Name属性,然后后台一一获取,这样也不方便扩展。
后来查了些资料后发现,关键点在于Checkbox的Name属性上。需要给checkbox一个统一的数组形式的Name,例如mycheckbox[],对的就是PHP中定义数组的一种方式。然后在后台获取这个数据,例如POST表单的$_POST[‘mycheckbox’],此时这个数据已经是一个数组了。下面是一个简单的例子。
Form表单代码
html
css
PHP表单处理
var_dump($_POST);
if (isset($_POST["hobby"])) {
foreach ($_POST["hobby"] as $hobby) {
echo "语言:{$hobby}
";
}
}
其实任何一组相同数据的文本框,都可以使用这种方式批量获取值,而不仅限于CheckBox。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
