I'm trying to show a specific item in the row when an item in the dropdown list is selected.
To clarify, lets say I have item1 chosen in the dropdown menu, and when item1 is chosen, I want price for item1 shown in another field on the page.
ps. I'm trying to make an inventory and ordering form which is connected to an MYSQL database.
Thanks in advance.
Here is my PHP code.
<?php
function dropdown( $alcohol, array $options, $selected=null )
{
/*** begin the select ***/
$dropdown = '<select name="'.$alcohol.'" id="'.$alcohol.'">'."\n";
$selected = $selected;
/*** loop over the options ***/
foreach( $options as $key=>$option )
{
/*** assign a selected value ***/
$select = $selected==$key ? ' selected' : null;
/*** add each option to the dropdown ***/
$dropdown .= '<option
`value="'.$key.'"'.$select.'>'.$option.'</option>'."\n";
}
/*** close the select ***/
$dropdown .= '</select>'."\n";
/*** and return the completed dropdown ***/
return $dropdown;
}
?>
<form>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('');
$sql = "SELECT alcohol FROM alcohol ";
$result = mysql_query($sql);
echo "<select name='alcohol'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>";
}
echo "</select>";
?>
</form>
//NEW STUFF BELOW THIS//
<form id="data" class="form_alcohol" role="form" method="post" action="connect.php">
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Submit">
<select size="1" name="alcohol">
<option value="">--- Select Alcohol ---</option>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('');
$sql = "SELECT alcohol FROM alcohol";
$result1 = mysql_query($sql);
while ($row = mysql_fetch_array($result1)) {
echo "<option value='" . $row['alcohol'] . "'>" . $row['alcohol'] . "</option>";
}
$dropdown1 = empty($_POST['alcohol'])? die ("ERROR: Select from dropdown") : mysql_escape_string($_POST['alcohol']);
echo "</select>";
?>
<?php
if(isset($_POST['Submit'])) {
mysql_connect('localhost', 'root', '');
mysql_select_db('');
$sql = "SElECT * FROM alcohol where '$dropdown1' = alcohol";
$result = mysql_query($sql) or die(mysql_error());
?>
<table>
<td> alcohol </td> <td> price </td> <td> amount in stock </td>
<?php
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>".$row['alcohol']."</td><td>".$row['price']."</td><td>".$row['quantity_in_stock']."</td>";
}
}
?>
</table>