0

I have created a simple dropdown using the code below.

<div class="row">
  <h2>Select Sample #1</h2>
  <select class="selectpicker" id="select1">
    <option>Mustard</option>
    <option>Ketchup</option>
    <option>Relish</option>
  </select>
</div>

Now need to create the dropdown in a way that I can enter new items to the dropdown so that it will be enabled to select the value and visible next time also.

1

2 Answers 2

0
<select class="selectpicker" id="select1">
    <?php foreach ($options as $optionValue => $optionName) { ?>
        <option value="<?= $optionValue ?>"> <?= $optionName ?> </option>
    <?php } ?>
</select>

If you want to add items to select in browser, you must do it with js because php is back end language.

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

1 Comment

yes.. My question is that how to add item using js.
0

it is so simple

<!DOCTYPE html>
<html>
<body>
<?php
$txts = ["Mustard","Ketchup","Relish"]; // put you dynamic array
?>
<select class="selectpicker" id="select1">
    <?php foreach ($txts as $txt) { ?>   // add Foreachloop for display object
        <option value="<?= $txt ?>"> <?= $txt ?> </option>
    <?php } ?>
</select>
</body>
</html>

if you need to add item than go for javascript

Comments

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.