How can PHP be used to calculate the total cost based on user input from a form?
To calculate the total cost based on user input from a form in PHP, you can retrieve the input values using $_POST or $_GET superglobals, perform any necessary calculations, and then display the result to the user.
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$quantity = $_POST['quantity'];
$price = $_POST['price'];
$totalCost = $quantity * $price;
echo "Total Cost: $" . $totalCost;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Quantity: <input type="text" name="quantity"><br>
Price: <input type="text" name="price"><br>
<input type="submit" value="Calculate Total Cost">
</form>
Keywords
Related Questions
- How can PHP developers ensure that user authentication processes are secure and compliant with best practices when using LDAP?
- How can PHP be used to create a speed test for a website?
- What are the best practices for implementing smooth transitions or animations between Powerpoint slides using PHP, HTML, and CSS?