[英]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.