Supposing we have the table browsers
in the database:
id | name
---+------------------
1 + Google Chrome
---+------------------
2 + Firefox
---+------------------
3 + Opera
---+------------------
4 + Safari
---+------------------
5 + Internet Explorer
---+------------------
We can generate the following HTML with PHP:
<?php
//...
$result = mysqli_query($mysqli, "SELECT `id`, `name` FROM `browsers`");
echo "<select name='browser'>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='{$row['id']}' label='{$row['name']}'>{$row['name']}</option>";
}
echo "</select>";
The HTML output will be:
<select name='browser'>
<option value='1' label='Google Chrome'>Google Chrome</option>
<option value='2' label='Firefox'>Firefox</option>
<option value='3' label='Opera'>Opera</option>
<option value='4' label='Safari'>Safari</option>
<option value='5' label='Internet Explorer'>Internet Explorer</option>
</select>
When the form is submitted, in PHP you can recover the attribute value value
selected. This value is the id
of the selected table record browsers
, then, to get the name of the selected browser, just select the respective database record:
<?php
// ...
if ($stmt = mysqli_prepare($mysqli, "SELECT `id`, `name` FROM `browsers` WHERE `id` = ?")) {
mysqli_stmt_bind_param($stmt, "i", $_POST["browser"]);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name);
mysqli_stmt_fetch($stmt);
echo "O navegador selecionado foi: ", $name;
}
The complete code tested was:
<form action="" method="post">
<select name="browser">
<?php
$mysqli = mysqli_connect("localhost", "root", "", "sopt");
$result = mysqli_query($mysqli, "SELECT `id`, `name` FROM `browsers`");
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='{$row['id']}' label='{$row['name']}'>{$row['name']}</option>";
}
?>
</select>
<button>Enviar</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($stmt = mysqli_prepare($mysqli, "SELECT `id`, `name` FROM `browsers` WHERE `id` = ?")) {
mysqli_stmt_bind_param($stmt, "i", $_POST["browser"]);
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $name);
mysqli_stmt_fetch($stmt);
echo "O navegador selecionado foi: ", $name;
}
}
?>
That? https://repl.it/J8L2/1
– MarceloBoni
Related: https://answall.com/a/54561/14262
– MarceloBoni
Or, https://repl.it/J8L2/3 I didn’t quite understand your question Leo, but that’s the way to go
– MarceloBoni
I would suggest creating the options using the same data source that generates the <option> in order to update the data in one place.
– Bacco
I didn’t get it right. Do you want to assemble the options from the database data? That’s it?
– user13603
@Marceloboni, I did not comment in the sense of denigrating, as may have been interpreted by the comments that have been removed, I think I expressed myself badly. I only meant that if you did not understand the question you could have simply asked for more information and then indicated related information. Even because you are part of the people I cherish on this site!
– user60252
downvoto is easy to give, it is difficult to explain why of the given downvoto. Worthy of pity this type of behavior. It does not add anything useful to the site or to who asked the question
– user60252
Just to clarify, I never downvoted your question, but I still found it a little confusing the way it was put. I couldn’t understand its purpose, really Leo... Perhaps it would explain its initial difficulty....
– MarceloBoni
@Marceloboni got a solution, see the answer
– user60252