PHP Practice Exercises
PHP Practice Exercises
<?php
//Sample Scripts
//Script name: PE1.php
End *****
<?php
//Sample Scripts
//Script name: PE2.php
End *****
+++++0+++++0+++++0+++++
<?php
//Sample Scripts
//Script name: SampleOutput_2.php
//Output from SampleInput_2.php
Sample Output: Output with invalid input for name (space is not
allowed) and age (period is not allowed)
+++++0+++++0+++++0+++++
<?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;
}
}
//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>";
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...";
}
?>
<br><br>
<input type=submit value="Compute" name=btnCompute>
<input type=reset value="Clear">
</form>
</center>
<?php
//Script name: GradeOutputTable.php
$Name = $_POST[txtName];
$Sub = $_POST[lstSub];
$Pre = $_POST[txtPre];
$Mid = $_POST[txtMid];
$Fin = $_POST[txtFin];
$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;
}
<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>
<?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>
<?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>";
<?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>
<?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>";
<?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>
<?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>";
<br><br>
<a href="GuestInputTable.php">Back </a>
</center>
<?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>
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>
<?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>";
$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>";
}
}
?>
<?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>
</table><br>
<br>
<input type="submit" value="Submit Order" name="Submit">
<input type="reset" value="Reset Order">
<form> ';
?>
<?php
//ScriptName: QuizOutputTable.php
?>
<html>
<head><title>Table Exercise</title></<head>
<body><center>
<?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>";
$prod="";
$price=0;
$tp=0;
if($_POST['chk1']=="p1")
{
$prod = "Bags";
$p = 2000;
$price = number_format($p,2);
echo " <tr> <td width=150>$prod</td>
<td width=100 align=right>$price</td>
</tr> ";
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);
$mode = "Charge: ";
}
<?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>
<?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>
<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> <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> <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> <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> <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>
<?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>
<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