0% found this document useful (0 votes)
11 views5 pages

Lab Work 2

The document outlines various PHP and MySQL exercises focused on managing HTML forms, including user registration and data validation. It provides code examples for handling form submissions, validating inputs, and calculating tax based on user input. Additionally, it includes an exercise for creating a speed converter program with error handling and output display requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Lab Work 2

The document outlines various PHP and MySQL exercises focused on managing HTML forms, including user registration and data validation. It provides code examples for handling form submissions, validating inputs, and calculating tax based on user input. Additionally, it includes an exercise for creating a speed converter program with error handling and output display requirements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP and MySQL for Dynamic Websites

Lab Work 2

1. Managing HTML Form with PHP (registration.html)


<html> Insert your name
<head>
<title>Registration Form</title> Print screen the
</head>
output
<body>

<form action="handle_registration.php" method="post">

<fieldset><legend>Enter your information in the form


below:</legend>

<p><b>First Name:</b> <input type="text" name="fname" /></p>


<p><b>Last Name:</b> <input type="text" name="lname" /></p>
<p><b>Phone No:</b> <input type="text" name="phone" /></p>
<p><b>Email Address:</b> <input type="text" name="email" /></p>

</fieldset>

<div align="left"><input type="submit" name="submit"


value="Register" /></div>

</form>
</body>
</html>
2. Managing HTML Form with PHP (handle_registration.php)
<html>
<head>
<title>Form Feedback</title>
</head>
<body>
<?php

// Create a shorthand for the form data.


$f = $_POST['fname'];
$l = $_POST['lname'];
$p = $_POST['phone'];
$e = $_POST['email'];

// Print the submitted information.


echo "<p>Thank you for your registration.<br /></p>
Below are your registered details:
<br /><b>First Name:</b>$f
<br /><b>Last Name:</b>$l
<br /><b>Phone No:</b>$p
<br /><b>Email:</b>$e";

?>
</body>
</html>

1
PHP and MySQL for Dynamic Websites

2. Form Data Validation (register.php)


<html>
<head>
<title>Registration Form</title>
</head>
<body>
<form action="handle_register.php" method="post">

<fieldset><legend>Enter your information in the form below:</legend>


<p><b>Name:</b> <input type="text" name="name" size="20" maxlength="40" /></p>
<p><b>Phone No:</b> <input type="text" name="phone" size="20" maxlength="40" /></p>
</fieldset>

<div align="left"><input type="submit" name="submit" value="Register" /></div>

</form>
</body>
</html>
Data Validation (handle_register.php)
<html>
<head>
<title>Form Feedback</title>
</head>
<body>
<?php

// Validate name
if (!empty($_POST['name'])) {
$name = $_POST['name'];
} else {
$name = NULL;
echo '<p><b>You forgot to enter your name!</b></p>';
}

// Validate phone number


if (!empty($_POST['phone'])) {
$phone = $_POST['phone'];
} else {
$phone = NULL;
echo '<p><b>You forgot to enter your phone number!</b></p>';
}

// If everything is okay, print the message.


if ($name && $phone) {

// Print the submitted information.


echo "<p>Thank you for your registration.<br /></p>
Below are your registered details:
<br /><b>Name:</b>$name
<br /><b>Phone No:</b>$phone";

} else {// One form element was not filled out properly.
echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}
?>

2
PHP and MySQL for Dynamic Websites

</body>
</html>
Try this! – add email and validate the email (print screen the output)

3. Creating tax calculator form (tax_form.php)


<html>
<head>
<title>Tax Form</title>
</head>
<body>
<form action="tax_result.php" method="post">

<fieldset><legend>Calculate tax:</legend>
<p><b>Quantity:</b> <input type="text" name="quantity" size="20" maxlength="40" /></p>
<p><b>Price(RM):</b> <input type="text" name="price" size="20" maxlength="40" /></p>
<p><b>Tax rate(%):</b> <input type="text" name="tax" size="20" maxlength="40" /></p>
</fieldset>

<div align="left"><input type="submit" name="submit" value="Calculate" /></div>

</form>
</body>
</html>

Display the tax (tax_result.php)


<html>
<head>
<title>Tax Result</title>
</head>
<body>
<?php

// Validate quantity
if (!empty($_POST['quantity'])) {
$quantity = $_POST['quantity'];
} else {
$quantity = NULL;
echo '<p><b>You forgot to enter your quantity!</b></p>';
}

// Validate price
if (!empty($_POST['price'])) {
$price = $_POST['price'];
} else {
$price = NULL;
echo '<p><b>You forgot to enter your price!</b></p>';
}

// Validate tax
if (!empty($_POST['tax'])) {
$tax = $_POST['tax'];
} else {
$tax = NULL;
echo '<p><b>You forgot to enter your tax!</b></p>';
}

// If everything is okay, print the message.

3
PHP and MySQL for Dynamic Websites

if ($quantity && $price && $tax) {

// Calculate the total.


$total = $quantity * $price;
$total = $total + ($total * ($tax/100)); // Calculate and add the tax.
$total = number_format($total, 2);

// Print the results.


echo 'You are purchasing <b>' . $quantity . '</b> widget(s) at a cost of <b>RM' . $price
. '</b> each. With tax, the total comes to <b>RM' . $total . '</b>.';

} else { // One form element was not filled out properly.


echo '<p><font color="red">Please go back and fill out the form again.</font></p>';
}
?>

</body>
</html>

Try this! Add your name and print the output

4. Do the following exercise:

Write a complete PHP codes named speed_converter.php as shown in Figure 1:


i) The program shall display an error message as shown in Figure 2 if the input is left blank and
the user enters invalid data type.
ii) The program shall display the conversion result Figure 3 if there are no error occurred.
iii) 1 MPH = 1.609 KPH
iv) You may design your form according to your creativity

4
PHP and MySQL for Dynamic Websites

Figure 1: Error Message (If input fields are left blank)

Figure 2: Successful Message

You might also like