生成复选框列表并在php/js中遍历它们的数组

生成复选框列表并在php/js中遍历它们的数组,php,javascript,arrays,post,checkbox,Php,Javascript,Arrays,Post,Checkbox,好的,我很困惑,在这个代码中查找了3个多小时 我生成一组复选框,如下所示: <?php while($row=mysql_fetch_array($result)){ // here generate some more html... echo '<table><tr><td><input type="checkbox" name ="ordered" onchange="checked()"/></td><

好的,我很困惑,在这个代码中查找了3个多小时

我生成一组复选框,如下所示:

<?php

while($row=mysql_fetch_array($result)){
// here generate some more html...   
    echo '<table><tr><td><input type="checkbox" name ="ordered" onchange="checked()"/></td></tr>'
echo "</table>";
?>
<head>
<script>
function removeDuplicateElement(arrayName){
  var newArray=new Array();
  label:for(var i=0; i<arrayName.length;i++ ){  
    for(var j=0; j<newArray.length;j++ ){
        if(newArray[j]==arrayName[i]) 
            continue label;
    }
    newArray[newArray.length] = arrayName[i];
  }
  return newArray;
}

var array = new Array();
function checked(){
var chkOrd = document.myForm.ordered;

    for (i=0; i < chkOrd.length; i++){
        if(chkOrd.checked){
            array.push(chkOrd[i]);
            //document.myForm.hid.value =chkOrd[i].value;
            document.myForm.hid.value += removeDuplicateElement(array);
            //alert("kliknuto je na " + array[1]);
        }
    }
}

</script>
</head>
我总是得到空数组,就像没有复选框传递给php一样。我知道php只发送复选框,但我的数组是空的,即使有复选框。。。我认为生成的复选框存在一些问题,因为我甚至无法获取单个复选框数组项


提前感谢您…

ahhh…如果您想提交复选框列表,请在name属性中添加[]。看看会发生什么。我已经试过了,但没有成功。但无论如何,我宁愿在js中完成这一部分,并通过隐藏字段将结果发布到php,因为这样我就可以按顺序点击复选框。正在执行…myForm.hid.value+=。。。我不会这么做的。您需要使用其中一个createElement方法,具体取决于您的库,或者如果您使用core JS。@ItayMov我可以只使用temp变量,并将值与以前的值连接起来,在元素之间插入'?然后拆分字符串,将其转换为数组…您完全可以将其作为一个连接的值提交,然后在服务器端进行处理。
$hid = $_POST['hid'];
echo $hid;