简体   繁体   English

HTML 表单,带有多值复选框

[英]HTML form with checkbox for multiple value

I have a Checkbox field in html input form.我在 html 输入表单中有一个复选框字段。 In this the value are fetched from another table.I want to select the multiple user with the help of this check box.What is wrong with my code在此值是从另一个表中获取的。我想在此复选框的帮助下 select 多个用户。我的代码有什么问题

<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>
<input type="checkbox" name="user" > <br>
   <checkbox value=""> </checkbox>
<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
 <?php }} ?>

I tried this code bu it is not showing the check box我试过这段代码但它没有显示复选框

Use input type Checkbox field Like this使用输入类型Checkbox字段像这样

<?php 

foreach($results as $result)
{               ?>  

<input type="checkbox" name="<?php echo $result->name; ?>" value="<?php echo $result->name; ?>"> <?php echo $result->name; ?> </br>

 <?php } ?>

<input type="checkbox">

Correct code:正确代码:

<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>

<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{
?>  
<input type="checkbox" name="user" value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?><br>
<?php
}}
  ?>

Edit : Try with multiple attribute of <select>编辑:尝试使用<select>multiple属性

<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
echo "<select name='user' multiple>";
foreach($results as $result)
{
?>  
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
<?php
}
echo "</select>";
}
?>

You can use bootstrap-multiselect Like This您可以像这样使用bootstrap-multiselect

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Dropdown Multi Select</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.12/css/bootstrap-multiselect.css" type="text/css" />


<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/js/bootstrap-multiselect.js"></script>

</head>

<body>

<form id="formone">

<div style="padding:20px">

<select id="chkone" multiple="multiple">

<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>  
<option value="<?php echo htmlentities($result->id);?>"><?php echo htmlentities($result->name);?></option>
 <?php }} ?>

</select>

</div>

</form>

</body>

<script type="text/javascript">

$(function() {

    $('#chkone').multiselect({

        includeSelectAllOption: true
    });

});

</script>
</html>

Just Try this试试这个

<div class="form-group col-md-6">
<label> Supervised BY( At IUAC)<span style="color:red;">*</span></label>

<?php 
$sql = "SELECT * from  tblstaff ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{
foreach($results as $result)
{               ?>
    <input type="checkbox" name="user[]" value="<?php echo htmlentities($result->id);?>" ><?php echo htmlentities($result->name);?>   
 <?php }} ?>

when you will submit the form, you will get $_POST['user'] as an array of user ids's that are checked.当您提交表单时,您将获得 $_POST['user'] 作为已检查的用户 ID 数组。

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

相关问题 反转提交时的复选框:仅在未选中复选框的情况下设置HTML表单值 - Invert checkbox on submit: Set HTML form value only if checkbox is unchecked HTML复选框,多个具有相同的“名称”。 如果选中,则值为1,如果未选中,则值为0 - Html Checkbox, multiple with same 'name'. Value 1 if checked, Value 0 if not checked 当表单中有多个复选框时,HTML复选框验证会中断 - HTML checkbox validation breaks when there are multiple checkboxes in the form 如何从html表单中获取多个复选框值 - how to get multiple checkbox values from html form HTML复选框为Javascript形式 - HTML checkbox to Javascript form Javascript document.getElementById function 未将复选框值返回到 HTML 表单 - Javascript document.getElementById function not returning checkbox value to HTML form 如何从 html 中的复选框表单中获取值? - how can I get the value from a checkbox form in html? 使用Request.Form获取禁用的HTML复选框的值 - Getting a value of a disabled HTML checkbox using Request.Form 使用AJAX,PHP和MYSQL在另一个复选框中显示html表单复选框中的多个值 - display multiple values from html form checkbox in another checkbox using AJAX, PHP and MYSQL 处理 HTML 中的多个复选框 - Processing multiple checkbox in HTML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM