0% found this document useful (0 votes)
49 views

PHP Practice Exercises

The document provides sample code for building forms and validating user input in PHP. It includes examples of using input tags, validation functions, and processing form data across multiple files. The exercises cover concepts like validating name, age, phone and calculating grades from exam scores.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

PHP Practice Exercises

The document provides sample code for building forms and validating user input in PHP. It includes examples of using input tags, validation functions, and processing form data across multiple files. The exercises cover concepts like validating name, age, phone and calculating grades from exam scores.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

System Development using PHP/MySQL Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

PRACTICE EXERCISE #1: Input Tags (Text, Submit, Reset)

***** Codes starts here:

<?php
//Sample Scripts
//Script name: SampleInput_1.php

//Sample Scripts without input validation


?>
DATA ENTRY <br><br>
<form action=SampleOutput_1.php method=post>
Enter your name: <input type=text name=txtName><br>
Enter your age: <input type=text name=txtAge><br>
Enter your phone no.: <input type=text name=txtPhone><br>
Enter your bill: <input type=text name=txtBill><br><br>

<input type=submit name=btnSubmit>


<input type=reset>
</form>

End *****

+++++0+++++0+++++0+++++

Sample output:

***** Codes starts here:


<?php
//Sample Scripts
//Script name: SampleOutput_1.php
//Output #1: Without input validation

echo "OUTPUT WITHOUT VALIDATION<br><br>";


echo "<b>Name: </b>" . $_POST['txtName'] . "<br>";
echo "<b>Age: </b>" . $_POST['txtAge'] . "<br>";
echo "<b>Phone: </b>" . $_POST['txtPhone'] . "<br>";
echo "<b>Bill: </b>" . $_POST['txtBill'] . "<br>";
?> End *****
System Development using PHP/MySQL Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

PRACTICE EXERCISE #2: Input Validation (ctype_alpha( ), ctype_digit( ), empty( ), is_string( ),


and is_numeric( ) functions)

***** Codes starts here:

<?php
//Sample Scripts
//Script name: SampleInput_2.php

//Sample Input with validation


?>
DATA ENTRY <br><br>
<form action=SampleOutput_2.php method=post>
Enter your name: <input type=text name=txtName><br>
Enter your age: <input type=text name=txtAge><br>
Enter your phone no.: <input type=text name=txtPhone><br>
Enter your bill: <input type=text name=txtBill><br><br>

<input type=submit name=btnSubmit>


<input type=reset>
</form>

End *****

+++++0+++++0+++++0+++++

***** Codes starts here:

<?php
//Sample Scripts
//Script name: SampleOutput_2.php
//Output from SampleInput_2.php

if(ctype_alpha($_POST['txtName'])) //Letters A-Z and a-z are accepted


$name = $_POST['txtName'];
elseif (!ctype_alpha($_POST['txtName']))
$name = "type letters a-z only";

if(ctype_digit($_POST['txtAge'])) // Only integers or whole number is accepted


$age = $_POST['txtAge'];
elseif(!ctype_digit($_POST['txtAge']))
$age = "type numbers 0-9 only";

if(empty($_POST['txtPhone'])) // Will test if the textbox is empty value


$phone = "phone number is empty";
elseif(is_string($_POST['txtPhone'])) // Will accept string values (any characters)
$phone = $_POST['txtPhone'];
System Development using PHP/MySQL Laboratory Practice Exercises

if(is_numeric($_POST['txtBill'])) // Only numbers are accepted (integer/floating point)


$bill = $_POST['txtBill'];
elseif(!is_numeric($_POST['txtBill']))
$bill = "letters and special characters are not allowed";

echo "OUTPUT WITH INPUT VALIDATION<br><br>";


echo "<b>Name:</b> $name <br>";
echo "<b>Age:</b> $age <br>";
echo "<b>Phone:</b> $phone <br>";
echo "<b>Bill:</b> $bill <br>";
?>

End *****

Sample Output: Output with invalid input and empty value:


System Development using PHP/MySQL Laboratory Practice Exercises

Sample Output: Output with invalid input for name only (space is not allowed)

Sample Output: Output with valid inputs

+++++0+++++0+++++0+++++
System Development using PHP/MySQL Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

PRACTICE EXERCISE #3:

***** Codes starts here:

<?php
Sample Output:
//Script name: Grade.php
?>

<h2>GRADE COMPUTATION </h2>


<br>
<form method="POST">
Student Name: <input type=textbox name=txtName><br>
Midterm Grade: <input type=textbox name=txtMid><br>
Final Grade: <input type=textbox name=txtFin><br><br>
<input type=submit value="Compute" name=btnCompute>
<input type=reset value="Clear">
</form>
<br>

<?php
if(isset($_POST['btnCompute']))
{
if(empty($_POST['txtMid']) or
empty($_POST['txtFin']))
{
echo "Please enter values...";
exit;
}
if(is_numeric($_POST['txtMid']) && is_numeric($_POST['txtFin']))
{
echo "<h2>GRADE INFORMATION</h2><br>";
echo "Student Name: " . $_POST['txtName'] . "<br>";
echo "Midterm Grade: " . $_POST['txtMid'] . "<br>";
echo "Final Grade: " . $_POST['txtFin'] . "<br>";
$semGrd = ($_POST['txtMid']+$_POST['txtFin'])/2;
echo "Semestral Grade: " . number_format($semGrd,2) . "<br>";
echo "Remarks: ";
if ($semGrd>=74.5)
echo "Passed";
else
echo "Failed";
}
else
echo "Enter numeric values only...";
}
?>

------------------------------------------------------ Start of GradeInput.php -----------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

<?pbp
//Script name: GradeInput.php Sample Output:
?>
<h2>GRADE COMPUTATION </h2>
<br>
<form action="GradeOutput.php" method="POST">
Student Name: <input type=textbox
name=txtName><br>
Midterm Grade: <input type=textbox
name=txtMid><br>
Final Grade: <input type=textbox
name=txtFin><br><br>
<input type=submit value="Compute"
name=btnCompute>
<input type=reset value="Clear">
</form>

------------------------------------------------------ End of GradeInput.php -----------------------------------------------------

----------------------------------------------------- Start of GradeOutput.php ----------------------------------------------------

<?php Sample Output:


//Script name: GradeOutput.php
if(isset($_POST['btnCompute']))
{
if(empty($_POST['txtMid']) or
empty($_POST['txtFin']))
{
echo "Please enter values...";
exit;
}

if(is_numeric($_POST['txtMid']) and
is_numeric($_POST['txtFin']))
{
echo "<h2>GRADE
INFORMATION</h2><br>";
echo "Student Name: " . $_POST['txtName'] . "<br>";
echo "Midterm Grade: " . $_POST['txtMid'] . "<br>";
echo "Final Grade: " . $_POST['txtFin'] . "<br>";
$semGrd = ($_POST['txtMid']+$_POST['txtFin'])/2;
echo "Semestral Grade: " . number_format($semGrd,2);
echo "<br>Remarks: ";
if ($semGrd>=74.5)
echo "Passed";
else
echo "Failed";
}
else
echo "Enter numeric values only...";
System Development using PHP/MySQL Laboratory Practice Exercises

}
?>

------------------------------------------------------ End of GradeOutput.php ----------------------------------------------------

--------------------------------------------------- Start of GradeInputTable.php -------------------------------------------------

<h2>GRADE COMPUTATION </h2>


<hr color=green size=2>
<form action="GradeOutputTable.php" method="POST"> Sample Output:
<table border=1>
<tr>
<td> Student Name:
<td> <input type=textbox name=txtName
size=30>
</tr>
<tr>
<td> Subject:
<td> <select name=lstSub>
<option> </option>
<option> Computer </option>
<option> Mathematics </option>
<option> English </option>
</select>
</tr>
<tr>
<td> Prelim Grade:
<td> <input type=textbox name=txtPre size=6>
</tr>
<tr>
<td> Midterm Grade:
<td> <input type=textbox name=txtMid size=6>
</tr>
<tr>
<td> Final Grade:
<td> <input type=textbox name=txtFin size=6>
</tr>
</table>

<br><br>
<input type=submit value="Compute" name=btnCompute>
<input type=reset value="Clear">
</form>
</center>

--------------------------------------------------- End of GradeInputTable.php -------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

-------------------------------------------------- Start of GradeOutputTable.php ------------------------------------------------

<?php
//Script name: GradeOutputTable.php
$Name = $_POST[txtName];
$Sub = $_POST[lstSub];
$Pre = $_POST[txtPre];
$Mid = $_POST[txtMid];
$Fin = $_POST[txtFin];

$Sem = $Pre * 0.25 + $Mid * 0.25 + $Fin * 0.5;


if ($Sem==100) Sample Output:
$Pt = 1.0;
else if($Sem>=95)
$Pt = 1.5;
else if($Sem>=90)
$Pt = 2.0;
else if($Sem>=85)
$Pt = 2.5;
else if($Sem>=80)
$Pt = 3.0;
else if($Sem>=75)
$Pt = 3.5;
else if($Sem>=70)
$Pt = 4.0;
else
$Pt = 5.0;

$Pt = number_format($Pt,2);

if ($Sem>=74.5)
$Rem = "Passed";
else
$Rem = "Failed";

if(isset($_POST['btnCompute']))
{
if(empty($_POST['txtPre']) or empty($_POST['txtMid']) or empty($_POST['txtFin']))
{
echo "<center><h2>Please enter grades...</h2></center>";
exit;
}

if(is_numeric($_POST['txtPre']) and is_numeric($_POST['txtMid']) and


is_numeric($_POST['txtFin']))
{
echo "<center><h2>GRADE INFORMATION</h2>
<hr color=green size=2>
<table border=0>
<tr>
<td> Student Name:
<td> <b>$Name</b>
System Development using PHP/MySQL Laboratory Practice Exercises

</tr>
</table>
<table border=1>
<tr>
<th width=100> Subject </th>
<th width=50> Prelim 25%</th>
<th width=50> Midterm 25%</th>
<th width=50> Final 50%</th>
<th width=50> Semestral Grade </th>
<th width=50> Point Equivalent </th>
<th> Remarks </th>
</tr>
<tr>
<td> $Sub
<td> $Pre
<td> $Mid
<td> $Fin
<td> $Sem
<td> $Pt
<td> $Rem
</tr>
</table>";
}
else
echo "<center><h2>Enter numeric values only...</h2></center>";
}
?>

<br>
<center>
<a href="GradeInputTable.php">Back
</center>

--------------------------------------------------- End of GradeOutputTable.php ------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

------------------------------------------------------- Start of GuestInput.php ----------------------------------------------------

<?pbp
//Script name: GuestInput.php Sample Output:
?>
<h2>GUEST INFORMATION </h2>
<br>
<form action="GuestOutput.php" method="POST">
Name (Last, First M.I.): <input type=textbox
name=txtName><br>
Age : <input type=textbox name=txtAge><br>
Gender: Male <input type=radio name=rdoGender
value="Male">
Female <input type=radio name=rdoGender
value="Famale"><br>
Course: <select name=lstCourse>
<option> </option>
<option>ACT</option>
<option>BSIT</option>
<option>BSCS</option>
</select>
<br>
Email Address: <input type=textbox name=txtEmail size=30>
<br><br>
<input type=submit value="Submit" name=btnSubmit>
<input type=reset value="Reset">
</form>

-------------------------------------------------------- End of GuestInput.php ----------------------------------------------------

------------------------------------------------------- Start of GuestOutput.php --------------------------------------------------

<?php
//Script name: GuestOutput.php Sample Output:
if(isset($_POST['btnSubmit']))
{
echo "<h2>GUEST INFORMATION</h2><br>";
echo "Name: " . $_POST['txtName'] . "<br>";
echo "Age: " . $_POST['txtAge'] . "<br>";;
echo "Gender: " . $_POST['rdoGender'] . "<br>";
echo "Course: " . $_POST['lstCourse'] . "<br>";
echo "Email: " . $_POST['txtEmail'] . "<br><br>";

echo "Your information has been saved...Thank you...";


}
?>
<br><br>
<a href="GuestInput.php">Back </a>

-------------------------------------------------------- End of GuestOutput.php --------------------------------------------------


---------------------------------------------------- Start of GuestInputTable.php ------------------------------------------------
System Development using PHP/MySQL Laboratory Practice Exercises

<?php
//Script name: GuestInputTable.php Sample Output:
?>
<center>
<h2>GUEST INFORMATION </h2>
<form action="GuestOutputTable.php" method="POST">
<table border=1>
<tr>
<td> Name (Last, First M.I.):
<td> <input type=textbox name=txtName>
</tr>
<tr>
<td> Age :
<td> <input type=textbox name=txtAge size=2>
</tr>
<tr>
<td> Gender:
<td> Male <input type=radio name=rdoGender value="Male">
Female <input type=radio name=rdoGender value="Famale">
</tr>
<tr>
<td> Course:
<td> <select name=lstCourse>
<option>ACT</option>
<option>BSIT</option>
<option>BSCS</option>
</select>
</tr>
<tr>
<td> Email Address:
<td> <input type=textbox name=txtEmail size=30>
</tr>
</table>
<br><br>
<input type=submit value="Submit" name=btnSubmit>
<input type=reset value="Reset">
</form>
</center>

---------------------------------------------------- End of GuestInputTable.php -------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

--------------------------------------------------- Start of GuestOutputTable.php -----------------------------------------------

<?php
//Script name: GradeOutputTable.php Sample Output:
$Name = $_POST['txtName'];
$Age = $_POST['txtAge'];
$Gender = $_POST['rdoGender'];
$Course = $_POST['lstCourse'];
$Email = $_POST['txtEmail'];

if(isset($_POST['btnSubmit']))
{
echo "
<center>
<h2>GUEST INFORMATION</h2>
<table border=1>
<tr>
<td> Name:
<td> $Name
</tr>
<tr>
<td> Age:
<td> $Age
</tr>
<tr>
<td> Gender:
<td> $Gender
</tr>
<tr>
<td> Course:
<td> $Course
</tr>
<tr>
<td> Email:
<td> $Email
</tr>
</table>";

echo "<br>Your information has been saved...Thank you...";


}
?>
<br><br>
<a href="GuestInputTable.php">Back </a>
</center>

--------------------------------------------------- End of GuestOutputTable.php ------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

----------------------------------------------- Start of GuestInputTableColumn.php --------------------------------------------

<?php
//Script name: GuestInputTableColumn.php Sample Output:
?>
<center>
<h2>GUEST INFORMATION </h2>
<form action="GuestOutputTableColumn.php" method="POST">
<table border=1>
<tr>
<td> Name (Last, First M.I.):
<td> <input type=textbox name=txtName>
</tr>
<tr>
<td> Age :
<td> <input type=textbox name=txtAge size=2>
</tr>
<tr>
<td> Gender:
<td> Male <input type=radio name=rdoGender
value="Male">
Female <input type=radio name=rdoGender
value="Famale">
</tr>
<tr>
<td> Course:
<td> <select name=lstCourse>
<option>ACT</option>
<option>BSIT</option>
<option>BSCS</option>
</select>
</tr>
<tr>
<td> Email Address:
<td> <input type=textbox name=txtEmail size=30>
</tr>
</table>
<br><br>

<input type=submit value="Submit" name=btnSubmit>


<input type=reset value="Reset">
</form>
</center>

----------------------------------------------- End of GuestInputTableColumn.php ---------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

---------------------------------------------- Start of GuestOutputTableColumn.php -------------------------------------------

<?php
//Script name: GradeOutputTableColumn.php
Sample Output:
$Name = $_POST['txtName'];
$Age = $_POST['txtAge'];
$Gender = $_POST['rdoGender'];
$Course = $_POST['lstCourse'];
$Email = $_POST['txtEmail'];

if(isset($_POST['btnSubmit']))
{
echo "
<center>
<h2>GUEST INFORMATION</h2>
<table border=1>
<tr>
<th> Name </th>
<th> Age </th>
<th> Gender </th>
<th> Course </th>
<th> Email </th>
</tr>
<tr>
<td> $Name
<td> $Age
<td> $Gender
<td> $Course
<td> $Email
</tr>
</table>";

echo "<br>Your information has been saved...Thank you...";


}
?>

<br><br>
<a href="GuestInputTable.php">Back </a>
</center>

---------------------------------------------- End of GuestOutputTableColumn.php -------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

-------------------------------------------------------- Start of QuizInput.php ----------------------------------------------------

<?php
//Script name: QuizInput.php

echo '
<form action="QuizOutput.php" method="POST">
<b>ON-LINE SHOPPING STORE</b> <br><br>
Select product here:<br>
<input type="checkbox" name="chk1" value="p1">Bags (Php 2,000.00)<br>
<input type="checkbox" name="chk2" value="p2">Shoes (Php 1,500.00)<br>
<input type="checkbox" name="chk3" value="p3">Accessories (Php 500.00)<br>
<input type="checkbox" name="chk4" value="p4">Jewelries (Php 1,000.00)<br><br>

Select payment mode:<br>


<input type="radio" name="rdo1" value="Cash" checked>Cash<br>
<input type="radio" name="rdo1" value="Card">Credit Card<br><br>

Customer Information:<br><br>
Name: <input type="text" name="CustName"><br>
Address: <input type="text" name="Address"><br>
Contact #:<input type="text" name="Contact"><br>
Card #:<input type="password" name="CardNum"><br><br>

<input type="submit" value="Submit Order" name="Submit">


<input type="reset" value="Reset Order">
<form> ';
?>

--------------------------------------------------------- End of QuizInput.php ----------------------------------------------------

------------------------------------------------------- Start of QuizOutput.php ---------------------------------------------------

<?php
//ScriptName: QuizOutput.php

if(isset($_POST['Submit']))
{
if (empty($_POST['CardNum']))
{
echo "<h1><center>Please enter your card number...</center></h1>";
}
else if($_POST['CardNum']=="1234")
{
echo "<b>CUSTOMER INFORMATION</b><br><br>";
echo "Name: " . $_POST['CustName'] . "<br>";
echo "Address: " . $_POST['Address'] . "<br>";
echo "Contact #: " . $_POST['Contact'] . "<br><br>";

echo "<b>ORDER INFORMATION</b><br><br>";


$p="";
System Development using PHP/MySQL Laboratory Practice Exercises

$t=0;
$ta=0;
$d=0;
if ($_POST['chk1']=="p1"){
$p = $p . "Bags............. Php 2,000.00 <br>";
$t = $t + 2000;}
if ($_POST['chk2']=="p2"){
$p = $p . "Shoes............ Php 1,500.00 <br>";
$t = $t + 1500;}
if ($_POST['chk3']=="p3"){
$p = $p . "Accessories...... Php 500.00 <br>";
$t = $t + 500;}
if ($_POST['chk4']=="p4"){
$p = $p . "Jewelries........ Php 1,000.00 <br>";
$t = $t + 1000;}

if ($_POST['rdo1']=="Cash"){
$d = $t * .10;
$ta = $t - $d;
$m = "Discount: ";}
else {
$d = $t * .10;
$ta = $t + $d;
$m = "Charge: ";}

echo "$p";
echo "<br>Sub Total: " . number_format($t,2) . "<br>";
echo $m . number_format($d,2) . "<br>";
echo "Total Purchase: " . number_format($ta,2);
}

else
{
echo "<h1><center>Please enter correct card number...</center></h1>";
}
}
?>

-------------------------------------------------------- End of QuizOutput.php ---------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

Sample Output: Sample Output:

----------------------------------------------------- Start of QuizInputTable.php -------------------------------------------------

<?php
//ScriptName: QuizInputTable.php

echo '<center>
<form action="QuizOutputTable.php" method="POST">
<b>ON-LINE SHOPPING STORE</b> <br><br>
Select product here:<br>
<table width="300" border=1 cellpadding="2" cellspacing="1">
<tr> <td><center>Product Listing</td>
<td><center>Price</td>
</tr>

<tr> <td><input type="checkbox" name="chk1" value="p1">Bags</td>


<td>Php 2,000.00 </td>
</tr>

<tr> <td><input type="checkbox" name="chk2" value="p2">Shoes</td>


<td>Php 1,500.00 </td>
</tr>

<tr> <td><input type="checkbox" name="chk3" value="p3">Accessories</td>


<td>Php 500.00 </td>
</tr>

<tr> <td><input type="checkbox" name="chk4" value="p4">Jewelries</td>


<td>Php 1,000.00 </td>
</tr>
System Development using PHP/MySQL Laboratory Practice Exercises

</table><br>

Select payment type:<br>


<table width="300" border=1 cellpadding="2" cellspacing="1">
<tr> <td><input type="radio" name="rdo1" value="Cash" checked>Cash</td>
<td>with 10% Discount</td>
</tr>

<tr> <td><input type="radio" name="rdo1" value="Card">Credit Card</td>


<td>with 10% Charge</td>
</tr>
</table><br>
Customer Information:<br>
<table width="300" border=1 cellpadding="2" cellspacing="1">
<tr>
<td width="100">Name: </td>
<td> <input name="CustName" type="text" size="30" maxlength="30"></td>
</tr>
<tr>
<td width="100">Address: </td>
<td> <input name="Address" type="text" size="30" maxlength="30"></td>
</tr>
<tr>
<td width=100">Contact #: </td>
<td> <input name="Contact" type="text" size="30" maxlength="30"></td>
</tr>
<tr>
<td width="100">Card #: </td>
<td> <input name="CardNum" type="password" size="30"
maxlength="30"></td>
</tr>
</table>

<br>
<input type="submit" value="Submit Order" name="Submit">
<input type="reset" value="Reset Order">
<form> ';
?>

----------------------------------------------------- End of QuizInputTable.php -------------------------------------------------

----------------------------------------------------- Start of QuizOutputTable.php -----------------------------------------------

<?php
//ScriptName: QuizOutputTable.php
?>
<html>
<head><title>Table Exercise</title></<head>
<body><center>
<?php
if(isset($_POST['Submit']))
System Development using PHP/MySQL Laboratory Practice Exercises

{
if(empty($_POST['CardNum']))
{
echo "<h1><center>Please enter your card number...</center></h1>";
}
else if($_POST['CardNum']=="1234")
{
echo "
<table width=250 border=1 cellpadding=2 cellspacing=1>
<th width=250>
<b>CUSTOMER INFORMATION</b>
</th>
</table>
<table width=250 border=1 cellpadding=2 cellspacing=1>
<tr>
<td width=100>Name: </td>
<td width=500>$_POST[CustName] </td>
</tr>

<tr>
<td width=100>Address: </td>
<td width=500>$_POST[Address] </td>
</tr>

<tr>
<td width=100>Contact #: </td>
<td width=500>$_POST[Contact] </td>
</tr>
</table><br>";

echo "<table width=270 border=1 cellpadding=2 cellspacing=1>


<th width=270>
<b>ORDER INFORMATION</b>
</th>
</table>";
echo "

<table border=1 cellspacing=2 cellpadding=2>


<tr>
<th width=150><font face=Arial, Helvetica, sans-serif> Product
Sold</font></th>
<th width=100><font face=Arial, Helvetica, sans-serif> Price</font></th>
</tr>";

$prod="";
$price=0;
$tp=0;
if($_POST['chk1']=="p1")
{
$prod = "Bags";
$p = 2000;
$price = number_format($p,2);
System Development using PHP/MySQL Laboratory Practice Exercises

echo " <tr> <td width=150>$prod</td>


<td width=100 align=right>$price</td>
</tr> ";

$tp = $tp + $p;


$tprice = number_format($tp,2);
}
if($_POST['chk2']=="p2")
{
$prod = "Shoes";
$p = 1500;
$price = number_format($p,2);
echo " <tr> <td width=150>$prod</td>
<td width=100 align=right>$price</td>
</tr> ";

$tp = $tp + $p;


$tprice = number_format($tp,2);
}
if($_POST['chk3']=="p3")
{
$prod = "Accessories";
$p = 500;
$price = number_format($p,2);
echo " <tr> <td width=150>$prod</td>
<td width=100 align=right>$price</td>
</tr> ";
$tp = $tp + $p;
$tprice = number_format($tp,2);
}
if($_POST['chk4']=="p4")
{
$prod = "Jewelries";
$p = 1000;
$price = number_format($p,2);
echo " <tr> <td width=150>$prod</td>
<td width=100 align=right>$price</td>
</tr> ";
$tp = $tp + $p;
$tprice = number_format($tp,2);
}

if($_POST['rdo1']=="Cash")
{
$disc_charge = number_format($tp * 0.10,2);
$totprice = number_format($tp - $disc_charge,2);
$mode = "Discount: ";
}
else if($_POST['rdo1']=="Card")
{
$disc_charge = number_format($tp * 0.10,2);
$totprice = number_format($tp + $disc_charge,2);
System Development using PHP/MySQL Laboratory Practice Exercises

$mode = "Charge: ";


}

echo " <table border=1 cellspacing=2 cellpadding=2>


<tr> <td width=150>Sub-Total:</td>
<td width=100 align=right>$tprice</td>
</tr>
<tr> <td width=150>$mode</td>
<td width=100 align=right>$disc_charge</td>
</tr>
<tr> <td width=150>Total Purchase</td>
<td width=100 align=right>$totprice</td>
</tr> </table>";
}
else
echo "<h1><center>Please enter correct card number...</center></h1>";
}
?>
</body>
</html>

----------------------------------------------------- End of QuizOutputTable.php -----------------------------------------------

Sample Output: Sample Output:


System Development using PHP/MySQL Laboratory Practice Exercises

Machine Problem #1: Answer

------------------------------------------------------------ Start of header.php ----------------------------------------------------

<?php
//Script Name: header.php
?>
<center>
<table width = "75%" border="0" cellspacing="2" cellpadding ="2">
<tr>
<th style = "font-family: Verdana; color: #006666; font-size: 20px" colspan = "8"> RICHARD GWAPO
COOPERATIVE INCORPORATED<br>
</th>
</tr>
<tr>
<th bgcolor = "#FFFFFF" style = "font-family: Verdana; color: #000006; font-size: 15px" colspan = "8">
Forever Gwapo St., Mandaluyong City <br>Telephone #: 143-4456
</th>
</tr>
</table></center><hr size=2 color=green noshade>

------------------------------------------------------------ End of header.php -----------------------------------------------------

---------------------------------------------------------- Start of registration.php -------------------------------------------------

<?php
include("header.php");
?>
<form action="information.php" method="POST">
<h1>REGISTRATION FORM:</h1>
<table>
<tr>
<td> <b>Complete Name:</b>
<td> <input type=textbox name=txtLast size=20 value="Last">,
<td> <input type=textbox name=txtFirst size=20 value="First">
<td> <input type=textbox name=txtMi size=2 value="M.I.">
</tr>
</table>
<table>
<tr>
<td> <b>Address:</b>
<td> <input type=textbox name=txtAdd size=50>
</tr>
<tr>
<td> <b>Contact Number:</b>
<td> <input type=textbox name=txtTel size=12 maxlength=12>
</tr>
System Development using PHP/MySQL Laboratory Practice Exercises

<tr>
<td> <b>Date of Birth:</b>
<td> <input type=textbox name=txtBirth size=12 maxlength=10 value="YYYY-MM-DD">
</tr>
<tr>
<td> <b>Gender:</b>
<td> Male <input type=radio name=rdoGender value="Male">
Female <input type=radio name=rdoGender value="Female">
</tr>
</table>
<table>
<tr>
<td> <b>Office Address:</b>
<td> &nbsp;&nbsp;<input type=textbox name=txtOffice size=30>
<td> <b>Phone Number:</b>
<td> <input type=textbox name=txtPhone size=12>
</tr>
<tr>
<td> <b>Position:</b>
<td> &nbsp;&nbsp;<input type=textbox name=txtPos size=30>
<td> <b>Employment Status:</b>
<td> <select name=lstStatus>
<option></option>
<option>Permanent</option>
<option>Casual</option>
</select>
</tr>
<tr>
<td> <b>Monthly Salary:</b>
<td> &nbsp;&nbsp;<select name=lstSalary>
<option value="Below Php10,000.00">Below Php10,000.00</option>
<option value="Php10,000.00 - Php20,000.00">Php10,000.00 –
Php20,000.00</option>
<option value="Above Php20,000.00">Above Php20,000.00</option>
</select>
</tr>
<tr>
<td> <b>Co-Borrower:</b>
<td> &nbsp;&nbsp;<input type=textbox name=txtCoBor size=30>
</tr>
</table>
<br><br>
<input type=submit name=btnSubmit value="Submit Registration">
<input type=reset name=btnClear value="Clear Form">
</form>

---------------------------------------------------------- End of registration.php --------------------------------------------------


System Development using PHP/MySQL Laboratory Practice Exercises

--------------------------------------------------------- Start of information.php--------------------------------------------------

<?php
include("header.php");
?>
<center>
<h2>Member Information</h2>
<?php

$Last = $_POST[txtLast];
$First = $_POST[txtFirst];
$Mi = $_POST[txtMi];
$Add = $_POST[txtAdd];
$Tel = $_POST[txtTel];
$Birth = $_POST[txtBirth];
$Gender = $_POST[rdoGender];
$Office = $_POST[txtOffice];
$Phone = $_POST[txtPhone];
$Pos = $_POST[txtPos];
$Status = $_POST[lstStatus];
$Sal = $_POST[lstSalary];
$CoBor = $_POST[txtCoBor];

if (isset($_POST[btnSubmit]))
{
echo "
<table border=1>
<tr>
<td width=100> Name:
<td width=300> $Last, $First $Mi.
</tr>
<tr>
<td> Address:
<td> $Add
</tr>
<tr>
<td> Telephone #:
<td> $Tel
</tr>
<tr>
<td> Date of Birth
<td> $Birth
</tr>
<tr>
<td> Gender:
<td> $Gender
</tr>
<tr>
<td> Office Address:
<td> $Office
</tr>
<tr>
System Development using PHP/MySQL Laboratory Practice Exercises

<td> Phone #:
<td> $Phone
</tr>
<tr>
<td> Position:
<td> $Pos
</tr>
<tr>
<td> Employment Status:
<td> $Status
</tr>
<tr>
<td> Monthly Salary:
<td> $Sal
</tr>
<tr>
<td> Co-Borrower:
<td> $CoBor
</tr>

</table>";
}
?>
<br>
<a href="registration.php">Back to Form

--------------------------------------------------------- End of information.php --------------------------------------------------

You might also like