Chapter 3 Forms
Forms.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Forms</title>
</head>
<body>
<form action="process.php" method="POST">
<p>
<label for="name">Name :</label>
<input type="text" name="fullname">
</p>
<p>
<label for="Email">Email</label>
<input type="email" name="email">
</p>
<p>
<label for="gender">Gender</label>
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
</p>
<p>
<label for="age_range">Age Range</label>
<select name="age">
<option value="0-20">0 to 20</option>
<option value="20-30">20 to 30</option>
<option value="30-40">30 to 40</option>
</select>
</p>
<p>
<label for="comments">Comments</label>
<textarea name="comments"></textarea>
</p>
<input type="submit" value="Register" name="submit">
</form>
</body>
</html>
process.php
<?php
echo "<h1>Processing Data</h1>";
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$age = $_POST['age'];
$comments = $_POST['comments'];
echo "<p>Your name is $fullname</p>";
echo "<p>Your email is $email</p>";
echo "<p>Your age is $age</p>";
echo "<p>Your comment is $comments</p>";
?>
Practice Activity
Compute the total price of the Fruit based on the price per kg and quantity use the formula (Total price = fruit price X quantity)
Myform.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Compute the Price</title>
</head>
<body>
<form action="calc.php" method="POST">
<p>
<label>Choose a fruit:</label>
<select name="fruit">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
</p>
<p>
Price per fruit: $
<input type="text" name="price">
</p>
<p>
<label for="quantity">Quantity:</label>
<input type="number" name="qty">
</p>
<input type="submit" value="Calculate Price">
</form>
</body>
</html>
Output Form
calc.php
<?php
echo "<h1> Fruit Price Calculation</h1>";
//Receive the fruit value
$fruit = $_POST['fruit'];
//Receive the price value
$price = $_POST['price'];
//Receive the quantity value
$qty = $_POST['qty'];
// Compute the total price
$total = $qty * $price;
echo "<p>You selected $qty $fruit(s) at $ $price each.</p>";
echo "<p>The Total Price is : $ $total</p>";
?>
Output Calculation