How I can insert values from database to <select> -> <option>
in HTML?
Example:
I have table with ID_class
and name of class
. And I would like to user can choose <option>
with value name of class
.
<select>
<option>VALUE FROM TABLE</option>
...
</select>
I work with PDO and jQuery.
It's not obvious where you're getting stuck, but here are some snippets to help out.
First of all, your select
needs a name and option
s need a value, so that the selected value can be accessed on the server side.
<select name='chooser'>
<option value='name-1'>...</option>
<option value='name-2'>...</option>
</select>
On the server side, your query should look something like this using PDO:
$insertData = $con->prepare('
INSERT INTO your_table
VALUES(
DEFAULT,
:name,
)
');
// the value of $_POST['chooser'] here is the value of the selected option (e.g. "name-2")
$insertData->bindValue('name', $_POST['chooser'], PDO::PARAM_STR);
$insertData->execute();