What are the best practices for incorporating radio buttons in PHP forms to select product prices?
When incorporating radio buttons in PHP forms to select product prices, it is important to ensure that only one option can be selected at a time. This can be achieved by giving each radio button the same name attribute but with different values. Additionally, the selected price option can be retrieved using PHP after the form is submitted.
<form method="post" action="process_form.php">
<input type="radio" name="price" value="10"> $10
<input type="radio" name="price" value="20"> $20
<input type="radio" name="price" value="30"> $30
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$selectedPrice = $_POST["price"];
echo "Selected price: $" . $selectedPrice;
}
?>