[英]Invert checkbox on submit: Set HTML form value only if checkbox is unchecked
[英]HTML form with checkbox for multiple value
我在 html 輸入表單中有一個復選框字段。 在此值是從另一個表中獲取的。我想在此復選框的幫助下 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 }} ?>
我試過這段代碼但它沒有顯示復選框
使用輸入類型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 } ?>
正確代碼:
<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
}}
?>
編輯:嘗試使用<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>";
}
?>
您可以像這樣使用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>
試試這個
<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 }} ?>
當您提交表單時,您將獲得 $_POST['user'] 作為已檢查的用戶 ID 數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.