简体   繁体   English

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

[英]how to access multiple checkboxes names while checkbox name is coming from database in php

Below code is showing some data from database, here in each row there is a checkbox having name of primaryid from database ..and when user click less than then 5 checkboxes a alert appear as show all the code is working .but 下面的代码显示了一些来自数据库的数据,在每一行中都有一个复选框,其名称来自数据库..并且当用户单击少于5个复选框时,将显示一个警报,表明所有代码都在工作。

Here I want that when user click 5 checkboxes i want the unique name of 5 checkboxes select and on submit button click and i want to do some php code with these 5 unique ids. 在这里,我希望当用户单击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>

Not sure if this is exactly what you want, but here I have made it so the checkboxes() function adds all the unique IDs to an array and then if more than 5 checkboxes have been clicked, POST them to the current page and you can access the IDs in PHP. 不知道这是否正是您想要的,但是在这里我已经做到了,所以checkboxes()函数将所有唯一的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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当多个复选框具有相同形式时,如何通过POST通过PHP脚本访问复选框 - How to access a checkbox with a PHP script via POST when there are multiple checkboxes in the same form 保留多个具有相同名称的复选框的复选框状态 - Keep checkbox status for multiple checkboxes with same name 如何检查是否存在多个具有相同名称的复选框或仅存在一个此类复选框? - How to check if multiple checkboxes with same name present or only one such checkbox present? 选中初始复选框时(如何使用PHP / Javascript)如何显示多个复选框 - How can I have multiple checkboxes appear when initial checkbox is checked (using PHP/Javascript) 当数据来自数据库时,如何从一组复选框中仅选择4个 - How we can choose only 4 from a group of checkbox when the data is coming from the database 如何在php中实现多个复选框 - how to implement multiple checkboxes in php 如何检查复选框列表中是否至少选中了一个复选框,其名称属性为数组 - How to check if at least one checkbox is checked from list of checkboxes with it name attribute as array 带有Javascript复选框的动态HTML表格难题-如何从函数外部访问checkbox.check? - Dynamic HTML table with Javascript checkboxes puzzle - how to access checkbox.checked from outside of the function? 当数据来自数据库时,如何检查html表中的CheckBox? 在asp.net MVC中 - How to Check CheckBox in html table when data is coming from Database? in asp.net MVC 如何从4个CheckBox中检查任何2个复选框? - how to Check any 2 checkBox from 4 CheckBoxes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM