KEMBAR78
PHP Practice Exercises | PDF | Computer Programming | Computer Science
100% found this document useful (1 vote)
336 views27 pages

PHP Practice Exercises

Uploaded by

dagsmagalona
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
100% found this document useful (1 vote)
336 views27 pages

PHP Practice Exercises

Uploaded by

dagsmagalona
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/ 27

UNIVERSITY OF MAKATI

PHP Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

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

***** Codes starts here:

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

//Sample Scripts without input validation


?>
DATA ENTRY <br><br>
<form action="SampleOutputPE1.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" name="btnReset">
</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 *****

Prepared by: Mr. Roel Richard G C. Traballo 1


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

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


preg_match() and is_numeric( ) functions)

***** Codes starts here:

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

//Sample Scripts with input validation


?>
DATA ENTRY <br><br>
<form action="SampleOutputPE2.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" name="btnReset">
</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

Prepared by: Mr. Roel Richard G C. Traballo 2


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

$phone = "phone number is empty";


elseif(is_string($_POST['txtPhone'])) // Will accept string values (any characters)
$phone = $_POST['txtPhone'];

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:

Prepared by: Mr. Roel Richard G C. Traballo 3


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

Sample Output: Output with invalid input for name (space is not
allowed) and age (period is not allowed)

Sample Output: Output with valid inputs

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

Prepared by: Mr. Roel Richard G C. Traballo 4


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

Name: ______________________________________ Rating: ___________


Course/Year & Section: ________________________

PRACTICE EXERCISE #3:


Sample Output:
***** Codes starts here:

<?php
//Script name: PE3.php
?>
<html>
<head>
<title>Grade Computation</title>
<style>
.error {color: #FF0000;}
</style>
</head>

<body>
<?php
//declare variables
$nameErr = $midErr = $finErr = "";
$name = $midGrade = $finGrade = $semGrd = $remarks = $fontColor = "";
$nameFlag = $midFlag = $finFlag = 0;

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
//check if name textfield is empty
if (empty($_POST['txtName']))
{
$nameErr = "Name is required.";
$nameFlag = 0;
}
else
{ $name = testInput($_POST['txtName']);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) //(!ctype_alpha($name))
{
$nameErr = "Only letters and white space allowed";
$nameFlag = 0;
}
}

if (!empty($name) and preg_match("/^[a-zA-Z ]*$/",$name))


$nameFlag = 1;

//check if midGrade textfield is empty


if (empty($_POST['txtMid']))
{

Prepared by: Mr. Roel Richard G C. Traballo 5


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

$midErr = "Midterm grade is required.";


$midGradeFlag = 0;
}
else
{ $midGrade = testInput($_POST['txtMid']);
// check if midterm Grade only contains numberss
if (!is_numeric($midGrade))
{
$midErr = "Only numbers are allowed.";
$midGradeFlag = 0;
}
else
if (($midGrade < 50) or ($midGrade > 100))
{
$midErr = "Midterm Grade must be between 50 - 100 only.";
$midGradeFlag = 0;
}
}

if (!empty($midGrade) and is_numeric($midGrade) and (($midGrade>=50) and ($midGrade


<=100)))
$midGradeFlag = 1;

//check if finGrade textfield is empty


if (empty($_POST['txtFin']))
{
$finErr = "Final grade is required.";
$finGradeFlag = 0;
}
else
{ $finGrade = testInput($_POST['txtFin']);
// check if final grade only contains numbers
if (!is_numeric($finGrade))
{
$finErr = "Only numbers are allowed.";
$finGradeFlag = 0;
}
else
if (($finGrade < 50) or ($finGrade > 100))
{
$finErr = "Final Grade must be between 50 - 100 only.";
$finGradeFlag = 0;
}
}
if (!empty($finGrade) and is_numeric($finGrade) and (($finGrade>=50) and ($finGrade <=100)))
$finGradeFlag = 1;

//compute for the semGrade


if (($nameFlag !=0) and ($midGradeFlag != 0) and ($finGradeFlag != 0))
{
$semGrd = ($midGrade+$finGrade)/2;

Prepared by: Mr. Roel Richard G C. Traballo 6


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

//determine remarks
if ($semGrd>=74.5)
$remarks = "Passed";
else
$remarks = "Failed";
}
}

function testInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>GRADE COMPUTATION </h2><br>
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p><span class="error">* required field.</span></p>
Student Name: <input type="textbox" name="txtName" value="<?php echo $name; ?>">
<span class="error"> * <?php echo $nameErr;?></span><br>
Midterm Grade: <input type="textbox" name="txtMid" value="<?php echo $midGrade; ?>">
<span class="error"> * <?php echo $midErr;?></span><br>
Final Grade: <input type="textbox" name="txtFin" value="<?php echo $finGrade; ?>">
<span class="error"> * <?php echo $finErr;?></span><br><br>
<input type="submit" value="Compute" name="btnCompute">
<input type="reset" value="Clear">
</form>
<br>
<?php
if (isset($_POST['btnCompute']) && ($nameFlag !=0) and ($midGradeFlag !=0) and ($finGradeFlag
!=0))
{
echo "<h2>GRADE INFORMATION</h2><br>";
echo "Student Name: ".$name."<br>";
echo "Midterm Grade: ".$midGrade."<br>";
echo "Final Grade: ".$finGrade."<br>";

echo "Semestral Grade: ".round($semGrd,2)."<br>";


if ($semGrd <= 74)
$fontColor = "#FF0000";
else
$fontColor = "#000000";
echo "Remarks: <font color=$fontColor>".$remarks."</font><br>";
}
?>
</body>
</html>

Prepared by: Mr. Roel Richard G C. Traballo 7


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

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

<?php Sample Output:


//Script name: GradeInput.php
?>
<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";
}

Prepared by: Mr. Roel Richard G C. Traballo 8


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

else
echo "Enter numeric values only...";
}
?>

------------------------------------------------------ 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 -------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 9


UNIVERSITY OF MAKATI
PHP 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:

Prepared by: Mr. Roel Richard G C. Traballo 10


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

<td> <b>$Name</b>
</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 ------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 11


UNIVERSITY OF MAKATI
PHP 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 --------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 12


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

---------------------------------------------------- Start of GuestInputTable.php ------------------------------------------------

<?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 -------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 13


UNIVERSITY OF MAKATI
PHP 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 ------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 14


UNIVERSITY OF MAKATI
PHP 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 ---------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 15


UNIVERSITY OF MAKATI
PHP 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 -------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 16


UNIVERSITY OF MAKATI
PHP 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>";

Prepared by: Mr. Roel Richard G C. Traballo 17


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

$p="";
$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 ---------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 18


UNIVERSITY OF MAKATI
PHP 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>

Prepared by: Mr. Roel Richard G C. Traballo 19


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

</tr>
</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>

Prepared by: Mr. Roel Richard G C. Traballo 20


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

<?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 "
<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")
{

Prepared by: Mr. Roel Richard G C. Traballo 21


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

$prod = "Bags";
$p = 2000;
$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['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: ";
}

Prepared by: Mr. Roel Richard G C. Traballo 22


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

else if($_POST['rdo1']=="Card")
{
$disc_charge = number_format($tp * 0.10,2);
$totprice = number_format($tp + $disc_charge,2);
$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:

Prepared by: Mr. Roel Richard G C. Traballo 23


UNIVERSITY OF MAKATI
PHP 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>

Prepared by: Mr. Roel Richard G C. Traballo 24


UNIVERSITY OF MAKATI
PHP 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 --------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 25


UNIVERSITY OF MAKATI
PHP 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>

Prepared by: Mr. Roel Richard G C. Traballo 26


UNIVERSITY OF MAKATI
PHP Laboratory Practice Exercises

<tr>
<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 --------------------------------------------------

Prepared by: Mr. Roel Richard G C. Traballo 27

You might also like