php中的foreach复选框POST [英] foreach checkbox POST in php

查看:27
本文介绍了php中的foreach复选框POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我的问题如下,如何在 PHP 中执行 $_POST 请求时选择已选中"复选框,目前我有复选框正在执行如下所示的数组.

Basically my question is the following, how can i select on "Checked" checkbox's while doing a $_POST request in PHP, currently i have the checkbox's doing an array as shown below.

<input type="checkbox" value="1" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="2" name="checkbox[]">
<input type="checkbox" value="3" name="checkbox[]">

我希望能够做这样的事情

I want to be able to do something like this

foreach(CHECKED CHECKBOX as CHECKBOX) {
   echo CHECKBOX VALUE;
}

我试过做类似的事情,但它没有回响任何东西.

I've tried doing similar to that and it's not echoing anything.

推荐答案

foreach($_POST['checkbox'] as $value) {

}

请注意,$_POST['checkbox']在至少一个复选框被选中时存在.因此,您必须在该循环之前添加一个 isset($_POST['checkbox']) 检查.最简单的方法是这样的:

Note that $_POST['checkbox'] will only exist if at least one checkbox is checked. So you must add an isset($_POST['checkbox']) check before that loop. The easiest way would be like this:

$checkboxes = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
foreach($checkboxes as $value) {
    // here you can use $value
}

这篇关于php中的foreach复选框POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆