NFSJ
NFSJ

Reputation: 101

How to checked the checkbox if my checkbox is using html and javascript?

How can i checked the checkbox if the data is already in the database as selected by user ? For normal html, i can use checked , however, im using javascript for this one. is there any way to edit the html instead of the javascript ?

 <td><div align="center"><span class="formlist">
      <select id="plant" name="plant[]" class="form-control" multiple="multiple">
        <?php 
                    $query_plant = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
                    $rs_plant = DB_Query($query_plant);

                    while ($row_plant = DB_FetchRow($rs_plant)) {

                        $plant.='<option value='.$row_plant["plant_id"].'>' .$row_plant["plant_name"].' ['.$row_plant["plant_id"].']</option>';

                        }   

                    mysql_free_result($rs_plant);
                    echo $plant;
                ?>
      </select>
    </span></div></td>

enter image description here

 <script type="text/javascript">
        $(function () {
            $('#plant').multiselect({
                includeSelectAllOption: true
            });
			 $('#btnSelected').click(function () {
                var selected = $("#plant option:selected");
                var message = "";
                selected.each(function () {
                    message += $(this).text() + " " + $(this).val() + "\n";
                });
                alert(message);
            });
            
               });
    </script>

Upvotes: 0

Views: 83

Answers (3)

NFSJ
NFSJ

Reputation: 101

 <tr>
    <td><div align="center">
      <select id="plant" name="plant[]" class="form-control" multiple="multiple" >
        <?php 
                    $query_po = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
                    $rs_po = DB_Query($query_po);

                    while ($row_po = DB_FetchRow($rs_po)) { ?>

                        <option value="<?php echo $row_po["plant_id"] ?>"
                        <?php 
                                $user_sql = "SELECT * FROM plant_access WHERE staff_id = '".$STAFF_ID."'";
                                $user_res = DB_Query($user_sql);

                                while($user_row = DB_FetchRow($user_res)) { 
                                    if($row_po["plant_id"] == $user_row["temp_plant_code"]){
                                        {
                                               echo " selected";
                                           } 


                                    } ?>


                                <?php
                                }   

                                     ?>
                                <?php echo $row_po["plant_name"] ?></option>

                    <?php
                    echo $row_po["plant_name"] ;
            } ?>
      </select>
    </div></td>
  </tr>

Credit to : @jYoThl

Upvotes: 0

JYoThI
JYoThI

Reputation: 12085

1) First you can add selected attribute to options which is user already selected and stored in database . like this

 $userPlants = [1,2,3,4];  //fetched value from database . previously user selected .

<option value="<?php echo $row_plant["plant_id"] ?>"

          <?php 

          if(in_array($row_plant["plant_id"], $userPlants)) 
           {
               echo " selected";
           } 

           ?>
><?php echo $row_plant["plant_name"] ?></option>

$(function () {
            $('#plant').multiselect({
                includeSelectAllOption: true
            });
            
	  // $("#plant").multiselect("refresh");  //if you need to refresh the multiselect use like this .

              });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"
    rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"
    rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"
    type="text/javascript"></script>
    

<td><div align="center"><span class="formlist">
      <select id="plant" name="plant[]" class="form-control" multiple="multiple">
<option value='option1'>option1</option>
<option value='option2' selected >option2</option>
<option value='option3' selected >option3</option>
<option value='option4'>option4</option>
<option value='option5'>option5</option>

                ?>
      </select>
    </span></div></td>

Upvotes: 1

T.Shah
T.Shah

Reputation: 2768

You can create an array of all plants that user has already selected. And then do it like this

  <td><div align="center"><span class="formlist">
  <select id="plant" name="plant[]" class="form-control" multiple="multiple">
    <?php        
            // query to fetch user palnts and crate an array
            $userPlants = [1231,1281,1241,1271];                       


                $query_plant = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
                $rs_plant = DB_Query($query_plant);

                while ($row_plant = DB_FetchRow($rs_plant)) {

                    $plant.='<option value='.$row_plant["plant_id"];

                    if (in_array($row_plant["plant_id"], $userPlants)) {
                        echo " checked ";
                    }

                    $plant.= '>' .$row_plant["plant_name"].' ['.$row_plant["plant_id"].']</option>';

                    }   

                mysql_free_result($rs_plant);
                echo $plant;
            ?>
  </select>
</span></div></td>

Upvotes: 1

Related Questions