How can a user select a number and display a calculation on the same page in PHP?
To allow a user to select a number and display a calculation on the same page in PHP, you can use a form with a dropdown menu for number selection and then process the calculation on the same page using PHP. You can use a conditional statement to check if the form has been submitted and then perform the calculation based on the selected number.
<?php
if(isset($_POST['submit'])){
$number = $_POST['number'];
$result = $number * 2; // Perform a simple calculation, e.g., multiply the selected number by 2
echo "The result of the calculation is: " . $result;
}
?>
<form method="post">
<label for="number">Select a number:</label>
<select name="number" id="number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<button type="submit" name="submit">Calculate</button>
</form>