What are some best practices for creating a form in PHP that includes dropdown menus for selecting products with different prices?

When creating a form in PHP that includes dropdown menus for selecting products with different prices, it is important to dynamically populate the dropdown options with the product names and prices from a database. This ensures that the prices are always up-to-date and accurate. Additionally, you should validate the selected product on the server-side to prevent any manipulation of the prices on the client-side.

<form method="post" action="process_form.php">
    <select name="product">
        <?php
        // Connect to database and fetch product names and prices
        $products = [
            'Product A' => 10.00,
            'Product B' => 20.00,
            'Product C' => 30.00
        ];

        // Populate dropdown options with product names and prices
        foreach ($products as $product => $price) {
            echo '<option value="' . $price . '">' . $product . ' - $' . $price . '</option>';
        }
        ?>
    </select>
    <input type="submit" value="Submit">
</form>