PHP:可以自动获取所有发布的数据并且取消选中多个复选框? [英] PHP: Possible to automatically get all POSTed data And Multiple checkbox unchecked?

查看:16
本文介绍了PHP:可以自动获取所有发布的数据并且取消选中多个复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们好,我有麻烦了.

Hello friends i have a trouble.

我正在尝试使用此代码来动态获取表单的变量和值,但是有无数个数字复选框,可能标记也可能未标记,我想知道如何关闭或0" 如果这不是一个复选框标记,这些数据已被使用 .ajax 和数据:

I'm trying to use this code to get dynamically, variables and values of a form, but there is a countless number checkbox, which may or not marked, I would like to know how can I get an off or "0" in case not this a checkbox labeled, these data have been used .ajax and data:

复选框的简短示例:

<input name="p-sis-0110-1" type="checkbox">
<input name="p-sis-0110-2" type="checkbox">
<input name="p-sis-0110-3" type="checkbox">
<input name="p-sis-0110-4" type="checkbox">
<input name="p-sis-0110-5" type="checkbox">
<input name="p-sis-0110-6" type="checkbox">

<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">
<input name="input[]" type="checkbox">

阿贾克斯:

.$("#formarea").serialize()

PHP:

foreach ($_POST as $key => $value){
    echo "Field ".htmlspecialchars($key)." is ".htmlspecialchars($value)."<br>"
}

感谢您帮助解决这个小困境.

I appreciate any help to solve this little dilemma.

推荐答案

仅提交成功"控件.未选中的复选框或单选按钮不是成功".

Only "successful" controls are submitted. An unchecked checkbox or radio button is not "successful".

您需要声明一个带有隐藏输入的默认值.确保隐藏输入出现在复选框之前,因此如果选中该复选框,它将覆盖默认隐藏输入,因为名称相同:

You need to declare a default value with a hidden input. Make sure the hidden input comes before the checkbox so if the checkbox is checked it will override the default hidden input since the names are the same:

<input name="p-sis-0110-1" type="hidden" value="0">
<input name="p-sis-0110-1" type="checkbox" value="1">

要使用数组,您需要明确定义索引,使它们相同:

To use an array you need to explicitly define the indexes so they are the same:

<input name="input[0]" type="hidden" value="0">
<input name="input[0]" type="checkbox" value="1">

这篇关于PHP:可以自动获取所有发布的数据并且取消选中多个复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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