PHP复选框数组验证 [英] PHP Checkbox array validating

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

问题描述

有一些复选框,我需要用'0'(未选中)和'1'(选中)来验证格式。

 < input type =checkboxname =subcategory1 []> Text 1< br> 
< input type =checkboxname =subcategory1 []> Text 2< br>
< input type =checkboxname =subcategory1 []> Text 3< br>

我尝试使用此代码验证它(在因特网上找到):

  $ sc1 =; ($ i = 0; $ i <= 2; $ i ++)

if(isset($ _ POST ['subcategory1'] [$ i])){$ sc1。 ='1'; }
else {$ sc1。='0'; }
}

首先这个效果很好,但是当我添加更多的复选框时,不工作。它在$ sc1的开头设置全部1。对它们进行计数。



可能存在3个div(一列为一个div)的问题,但仍然有相同的名称?



Edit1:$ sc1 chould看起来像010

解决方案

这是因为您使用的是$ sc1。=在循环。你正在添加你的价值。所以一旦它设置为1,任何0将被添加。
摆脱循环中的所有停顿(。= should be be =)

update:你应该把键放到数组中

 < input type =checkboxname =subcategory1 [1]>文字1< br> 
< input type =checkboxname =subcategory1 [2]> Text 2< br>
< input type =checkboxname =subcategory1 [3]>文字3< br>


Have some checkboxes, which I need to validate into format with '0' (unchecked) and '1' (checked).

<input type="checkbox" name="subcategory1[]" >Text 1<br>
<input type="checkbox" name="subcategory1[]" >Text 2<br>
<input type="checkbox" name="subcategory1[]" >Text 3<br>

I tried validate it with this code (found on internet):

$sc1 = "";
for($i=0; $i<=2; $i++)
  {
  if(isset($_POST['subcategory1'][$i])){ $sc1 .= '1'; }
  else { $sc1 .= '0'; }   
  }

Firstly that works well, but when I add more checkboxes, this isn't working. It set all "1" at the begining of $sc1. Count them well.

Could be problem that they are in 3 divs (one div for one column), but still with same name?

Edit1: $sc1 chould looks like 010

解决方案

it's because you're using $sc1 .= in the loop. you're adding your values. so once it' set to 1, any 0 will just be added. get rid of the full stops in your loop (.= should just be =)

update: you should put the keys in the array

<input type="checkbox" name="subcategory1[1]" >Text 1<br>
<input type="checkbox" name="subcategory1[2]" >Text 2<br>
<input type="checkbox" name="subcategory1[3]" >Text 3<br>

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

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