0

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>

10
  • Checkbox or dropdown? Commented May 15, 2017 at 4:17
  • if($option==$db_stored_option) { echo "checked" } Commented May 15, 2017 at 4:19
  • @ArunKumaresh dropdown with checkbox. Commented May 15, 2017 at 4:24
  • You are not cleared with the problem because you said you want to check the checkbox but the code shows the dropdown. First describe the problem correctly so that the readers can contribute to your question. Commented May 15, 2017 at 4:24
  • @YashParekh I have updated the question with pictures Commented May 15, 2017 at 4:27

3 Answers 3

1

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>

Sign up to request clarification or add additional context in comments.

4 Comments

Im sorry for taking quite a time to try this code as i was kinda busy with other codes. but i will try this code and get back to you soon. thank you very much! :D
i read documentation and prepared this answer hopefully it will help you @NFSJ
you need to place the selected property based on that if condition @NFSJ
glad to help you @NFSJ
1

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>

2 Comments

the main point is the checked keyword right ? it is not working. ive tried before using the checked keyword but it didnt work..
Thank you for this as i finally able to understand more and modified this code and finally getting the results. Thanks!
0
 <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

1 Comment

This is my modified code that answers what i want :)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.