Convert From Feet to Meters Using PHP Script Code With HTML

Hello Friends Today, through this tutorial, I will tell you How can i Convert From Feet to Meters Using PHP Script Code With HTML.

Here’s the HTML and PHP code for a Feet to Meters converter:

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Feet to Meters Converter</title>
</head>
<body>
<h1>Feet to Meters Converter</h1>
<form method="post" action="process.php">
<label for="feet">Enter value in feet:</label>
<input type="number" name="feet" id="feet" required>
<br>
<button type="submit">Convert</button>
</form>
</body>
</html>

process.php

<?php
// Define conversion factor
$feet_to_meter = 0.3048;
// Check if form is submitted
if (isset($_POST['feet'])) {
$feet = (float) $_POST['feet'];
$meters = $feet * $feet_to_meter;
} else {
$feet = 0;
$meters = 0;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Feet to Meters Converter</title>
</head>
<body>
<h1>Feet to Meters Converter</h1>
<?php if (isset($_POST['feet'])): ?>
<p><b><?php echo $feet; ?> feet is equal to <?php echo $meters; ?> meters.</b></p>
<?php endif; ?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="feet">Enter value in feet:</label>
<input type="number" name="feet" id="feet" value="<?php echo $feet; ?>" required>
<br>
<button type="submit">Convert</button>
</form>
</body>
</html>

Explanation:

1. The index.html file defines the basic structure with a heading and a form to enter the value in feet.
2. The process.php file handles the form submission and conversion:
– Defines the conversion factor for feet to meters.
– Checks if the form is submitted using `isset()`.
– Retrieves the value from the submitted form using `$_POST[‘feet’]`.
– Performs the conversion by multiplying the feet by the conversion factor.
– Displays the result using an HTML structure with conditional statements based on form submission.

This code demonstrates how to create a simple Feet to Meters converter using HTML and PHP, with features like pre-filling the input field and displaying the converted value.