0

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>

1 Answer 1

1

First of all, use mysqli. Mysql_* is deprecated and will be removed in a future version.

Now that we have that out of the way, I would add a "marker" character in the original dropdown select values in order to get the value. Pick a Unicode character that wouldn't normally be in your database, and echo that in the value so it looks like this:

<option value="$name@$price">$name</option>

Then in the code above, split the value at the @ character and pick out what is after it in order to get the secondary value. That should save you a database operation in the long run.

Good luck!

Sign up to request clarification or add additional context in comments.

3 Comments

I'm not following, I added what you said and set $name = $result. But I get an error as follows "Parse error: syntax error, unexpected '$name' (T_VARIABLE), expecting ',' or ';' in C:\wamp\www\connect.php on line 53"
additionally, when I switch to msqli, it will not connect to my database.
Are you using $con = mysqli_connect($host,$user,$pass,$database); ? Also, you are missing a semicolon somewhere is the parser says it was expecting one. Check the line above. And you need to fetch array before just applying your MySQL result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.