当复选框名称来自php中的数据库时,如何访问多个复选框名称

时间:2015-11-26 18:39:34

标签: javascript php jquery mysql checkbox

下面的代码显示了数据库中的一些数据,每行中都有一个复选框,其名称为来自数据库的primaryid ..当用户点击少于5个复选框时,会出现一个警告,显示所有代码都在工作。但是/ p>

这里我希望当用户点击5个复选框时,我想要5个复选框的唯一名称选择并单击提交按钮,我想用这5个唯一ID执行一些PHP代码。

$sql="some sql";
$result = mysqli_query($mysql,$sql);

while($res1=mysqli_fetch_array($result,MYSQLI_ASSOC)) {


                    ?>
                                <tr>

                                <td>

                                <input type="checkbox" name="studentid"  value="<?php echo $res1['leadID']?>" ></td>


                <td>some stuff here </td>
                              </tr>
                    <?php }
                                ?>     

<input type="submit" name="submit" value="Submit" class="btn newcol1" id="submit2"  onClick="checkboxes();">


                 <script>
     function checkboxes()
      {

       var inputElems = document.getElementsByTagName("input"),
        count = 0;

        for (var i=0; i<inputElems.length; i++) {       
           if (inputElems[i].type == "checkbox" && inputElems[i].checked == true){
              count++;

           }

        }
        if(count < 5){
                  alert ("Select at least 5 leads");
                  }




  } </script>

1 个答案:

答案 0 :(得分:0)

不确定这是否正是您想要的,但是我已经这样做了,所以复选框()函数将所有唯一ID添加到数组中,然后如果单击了超过5个复选框,则将它们发布到当前页面并且您可以在PHP中访问ID。

function checkboxes(){
    var inputElems = document.getElementsByTagName("input"),
    count = 0;

    var ids = []
    for (var i=0; i<inputElems.length; i++) {       
        if (inputElems[i].type == "checkbox" && inputElems[i].checked == true){
            ids.push(inputElems[i].getAttribute("value"));
            count++;
        }
    }

    if(count < 5){
        alert ("Select at least 5 leads");
    }
    else{
        //more than 5 checkboxes selected
        post(window.location.href, {ids: ids});
    }
}

function post(path, params, method) {
    method = method || "post"; // Set method to post by default if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        if(params.hasOwnProperty(key)) {
            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", key);
            hiddenField.setAttribute("value", params[key]);

            form.appendChild(hiddenField);
         }
    }

    document.body.appendChild(form);
    form.submit();
}