1

I have a Checkbox field in html input form. 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

<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

1
  • 1
    <option> tag is for the <select> statement, quite different from a checkbox. Commented Oct 24, 2019 at 4:41

4 Answers 4

3

Use input type Checkbox field Like this

<?php 

foreach($results as $result)
{               ?>  

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

 <?php } ?>
Sign up to request clarification or add additional context in comments.

Comments

1

<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>

<?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>";
}
?>

6 Comments

it is working fine but it display the complete list. i want it should come in dropdown with check box.I tried with selectt option but no joy
any one having any idea plz
any more suggestion plz
I think you are telling about multiple attribute of <select> tag
but it will not show the check box.It requires the Ctrl key to select multiple
|
1

You can use bootstrap-multiselect Like This

<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>

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.