Hello Friends Today, through this tutorial, I will tell you Convert from Inches to centimeters php script code with html.
Here's the code to convert inches to centimeters using PHP with HTML:
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Inches to Centimeters Converter</title> </head> <body> <h1>Inches to Centimeters Converter</h1> <form action="converter.php" method="post"> <label for="inches">Enter value in Inches:</label> <input type="number" name="inches" id="inches" required> <br> <input type="submit" value="Convert"> </form> </body> </html>converter.php
<?php
// Get the input value from the form
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$inches = $_POST['inches'];
} else {
// Handle potential errors or display a message if no value is submitted
echo "Please enter a value in inches.";
exit;
}
// Conversion factor: 1 inch = 2.54 centimeters
$centimeters = $inches * 2.54;
// Display the result
echo "<h2>Result</h2>";
echo "<p>$inches inches is equal to $centimeters centimeters.</p>";
?>
Explanation:-
1. index.html:- This file creates a simple form with a label and an input field for entering the value in inches. It includes a submit button.
2. converter.php:- This file processes the form submission.
- It checks if the request method is POST, meaning the form was submitted.
- If the method is POST, it retrieves the value from the `inches` input field using `$_POST['inches']`.
- It performs the conversion using the formula `$centimeters = $inches * 2.54`.
- Finally, it displays the result using HTML elements.