Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code to populate in COMBOBOX the contents of a MYSQL table

mysql_connect('localhost', 'root', '');
mysql_select_db('test');
$sql = "select id, descr from firm order by id";
$records = mysql_query($sql) or die(mysql_error());
echo "<select fv>";
while($row = mysql_fetch_array($records))
{
$fv = $row['descr'];
echo "<option value='".$fv."'>".$fv."</option>";
//echo "<option value='".$row['id']."'>".$row['descr']."</option>";
}

echo "</select>";
echo "
";

mysql_free_result($records);
?>

I'm looking for a solution to get the selected value in a PHP variable for later use.

Any suggestion will be appreciated


* Table firms consists from two colums id and descr
Posted
Updated 26-Mar-21 8:05am
v2

echo "<option value='".$fv."'>".$fv."</option>";
 can be more simply written as:
echo "<option value='$fv'>$fv</option>";


php is a server-side language - you need to get the value to the server to map it into a php symbol.

Now, for your question, getting the value from the client side to the server side will take use of either a <form> with your select inside, and the value will turn up in $_REQUEST array on the server side (target should be .php) when the form is submitted.

Alternatively, you can use AJAX to send the value to the server when some event of your choosing occurs - without modifying the current page on the user side if you don't want to. This route requires javaScript.
 
Share this answer
 
v2
Add a name to your combo select to call it easier

echo "";
while($row = mysql_fetch_array($records))
{
$fv = $row['descr'];
echo "".$fv."";
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900