John Davidson

PHP calculator width html form

0 comments
Message:


can't figure out why my form will not work.


it has to calculate area of a rectangle.
Form has to disappear after submit and show answer in a sentence.


php code has to check if form is filled and submitted, then hide it and echo the answer,
I'm just learning, so sorry if it seems too easy of an issue.


<?php
if(isset($_POST['submit'])) {

if(!isset($_POST['length'], $_POST['width']))

{
function area($a, $b) {
$sum = $a * $b;
echo "rectangle, with $a cm in length and $b cm in width, area is $sum square cm.";}
area($_POST['length'], $_POST['width']);
} }

else { ?>
<form action="<?php $_PHP_SELF; ?>" method="POST">
Length: <input type="text" name="length" />
Width: <input type="text" name="width" />
<input type="submit">
</form>
<?php } ?>


My Answer:
Here is a simple example of a PHP calculator for calculating the area of a rectangle using an HTML form:

html



Rectangle Area Calculator



if ($_SERVER["REQUEST_METHOD"] == "POST") {
$length = $_POST["length"];
$width = $_POST["width"];
$area = $length * $width;
echo "The area of the rectangle is: " . $area;
} else {
?>















}
?>





In this code, we first check if the form has been submitted using `$_SERVER["REQUEST_METHOD"] == "POST"`. If it has, we calculate the area of the rectangle and display it. If not, we display the form for the user to input the length and width of the rectangle.

When the form is submitted, the page will reload and display the calculated area of the rectangle. The form will disappear as the PHP code will only display the form if the request method is not POST.

I hope this helps! Let me know if you have any questions.

Rate this post

3 of 5 based on 5522 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us