keenan cameron
keenan cameron

Reputation: 11

Select an option in a dropdown box and display its associated data in php?

  1. How to select a supplement and display its associated data such as its description and cost.
  2. Any help will be appreciated.

PHP:

require('database.php');

$suppID = filter_input(INPUT_POST, 'suppID');

if($suppID==null || $suppID==false){
    $suppID = 'Supplement-1';
}

$query = 'select * from tblsupplements where Supplement_id= :SupplementID';
$statement = $db->prepare($query);
$statement = bindValue(':SupplementID', $suppID);
$statement->execute();
$supplements = $statement->fetchAll();
$statement->closeCursor();

HTML:

<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement): ?>          
<option value="<?php echo $supplement['Supplement_id']; ?>">
<?php echo $supplement['Supplement_id']; ?>
</option>         
<?php endforeach; ?>
</select>

Upvotes: 1

Views: 57

Answers (1)

Sanasar Yuzbashyan
Sanasar Yuzbashyan

Reputation: 764

Try this, hopefully helps.

<label>Supplement ID:</label>
<select name='suppID'>
<?php foreach ($supplements as $supplement) {
    echo "<option value='".$supplement['Supplement_id']."'>".$supplement['Supplement_id']."</option>";
}
?>
</select>

Upvotes: 1

Related Questions