John Davidson

javascript - I am using selectpicker I want to edit the data coming from the database, but all options are coming already selected

0 comments
Message:


all categories coming selected which i want to see only which information is coming from database.
sorry for my english.


<div class="col-xs-4">
<label>Categories</label>
<!-- SEARCH JS find (selectpicker) -->
<select name="product_cat" id="product_cat" class="form-control selectpicker" data-live-search="true" multiple>
<?php

while($productcatcek=$productcatsor->fetch(PDO::FETCH_ASSOC)) {

?>

<option selected="">
<dl>
<dt ><?php
if ($productcatcek['pcat_id']==$productcatcek['pcat_id']) {
echo $productcatcek['pcat_name'];
}
?> - </dt>
<dd <?php echo $productcatcek['pcat_id']==$productcatcek['pcat_id'] ? 'selected=""' : '' ?>><?php echo $productcatcek['spcat_name']; ?></dd>
</dl>
</option>

<?php } ?>
</select>
<!-- SEARCH Finish -->
</div>

SOLUTION


<option value="<?php echo $productcatcek['spcat_id']; ?>" <?php echo $productcatcek['spcat_id']==$productcek['product_cat'] ? 'selected=""' : '' ?>>
<dl>
<dt><?php echo $productcatcek['pcat_name']; ?> - </dt>
<dd><?php echo $productcatcek['spcat_name']; ?></dd>
</dl>
</option>


My Answer:
To display only the selected options that are coming from the database using selectpicker in JavaScript, you can follow these steps:

1. Retrieve the data from the database and store it in a variable.
2. Populate the select element with the options from the database.
3. Initialize the selectpicker plugin on the select element.
4. Use JavaScript to set the selected options based on the data retrieved from the database.

Here is an example code snippet to achieve this:

html





In this code snippet, we first retrieve the selected options from the database and store them in the `selectedOptions` array. We then loop through the options in the select element and set the `selected` property to true for the options that match the selected options from the database.

Finally, we initialize the selectpicker plugin on the select element to enhance its functionality.

Rate this post

3 of 5 based on 9342 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us