-1

is there a way to get all the values of < option > loaded from a database if I choose ALL-of-the-ABOVE option?

<select id="cars">
 <option value="Volvo">Volvo</option>
 <option value="Saab">Saab</option>
 <option value="Opel">Opel</option>
 <option value="Audi">Audi</option>
 <option value="All">All of the Above</option>
</select>

Thank you in advance. :)

By the way my actual code goes like this:

<select name="exam_class_id" id="exam_class_id" class="form-control" required>
<option value="">Select Class</option>
<?php
foreach($result as $row)
{
echo '
<option value="'.$row["class_id"].'">'.$row["class_name"].'</option>
';
}
?>
</select>

I kinda want it to have an < option > to select ALL CLASSES.

  • Does this answer your question? [How can I get a list of all values in select box?](https://stackoverflow.com/questions/18113495/how-can-i-get-a-list-of-all-values-in-select-box) – Rahul Sep 02 '21 at 13:17
  • Can you clarify what you mean by `loaded from a database`? This sounds like it might be some server-side logic on form submit to get all of the other values if `All` is submitted. – WOUNDEDStevenJones Sep 02 '21 at 13:18
  • i mean the < options > that will be displayed are from a db, and i want it to have an < option > in which i select it, all the loaded < options > in the < select > are the ones to be used as variables. – CleverNever Sep 02 '21 at 13:21

2 Answers2

1

The browser will only send the selected value ("All") so you can't get them from the request under normal circumstances.

The usual approach to solving this problem would be to have PHP get the list of values from whereever it got them when it generated the HTML containing the <select> element in the first place.

Typically this will be a database, and if you've hardcoded them into the HTML you should consider moving them to a database (or at least an array in a shared include). … but you've said they are in a database already so you can just crib you existing code that generated the HTML.


The nastier approach would be to add them to the request.

e.g.

<option value="Volvo,Saab,Opel,Audi">All of the Above</option>

or as hidden inputs.

And that could be done dynamically with JS (which could read the unselected option elements to do it) or PHP.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Try this code snippet you can use implode to do this job

<select name="exam_class_id" id="exam_class_id" class="form-control" required>
<option value="">Select Class</option>
<?php
foreach ($result as $row) {
    echo ' <option value="' . $row["class_id"] . '">' . $row["class_name"] . '</option> ';
}
?>
<option value="<?= implode(',', $result) ?>">All</option></select>
Abdul Rahman
  • 51
  • 1
  • 6