Download File => Click
Practice Programs:
1. To design an application that works as a simple calculator using PHP.
(use isset()).
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Calculator</title>
<style>
label {
margin-right: 1rem;
}
input{
display: block;
margin: 1rem 0;
}
</style>
</head>
<body>
<h1>Simple
Calculator</h1>
<form action="#"
method="post">
<label
for="">Enter First Number : </label>
<input
type="number" name="n1" id="">
<label
for="">Enter Second Number : </label>
<input
type="number" name="n2" id="">
<select
name="choice" id="">
<option
value="">Select the operation</option>
<option
value="1">Addition</option>
<option
value="2">Subtraction</option>
<option
value="3">Multiplication</option>
<option
value="4">Division</option>
</select>
<input
type="submit" value="result" name="result">
</form>
<?php
if(isset($_POST['result'])) {
$n1 = $_POST['n1'];
$n2 = $_POST['n2'];
$operation =
$_POST['choice'];
switch($operation) {
case 1:
$result = $n1 + $n2;
echo "Addition =
$result";
break;
case 2:
$result = $n1 - $n2;
echo
"Subtraction = $result";
break;
case 3:
$result = $n1 * $n2;
echo
"Multiplication = $result";
break;
case 4:
$result = $n1 / $n2;
echo "Division =
$result";
break;
}
}
?>
</body>
</html>
=================================================================
2. Write a PHP script to check PAN number entered by the customer is valid
or not and display an appropriate message.
<?php
// function definition
function isPanNumber($num) {
$pan_pattern =
'/^[A-Z]{5}[0-9]{4}[A-Z]$/';
if(preg_match($pan_pattern,
$num)) {
return true;
}
else {
return false;
}
}
$user_pan =
"CEGAA7906R";
if(isPanNumber($user_pan)) {
echo "PAN number is
valid !! : $user_pan <br>";
}
else {
echo "PAN number is not
valid !! : $user_pan <br>";
}
?>
=====================================================================
3. Write a PHP script to check mobile number entered by the user is valid
or not and display an appropriate message.
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Mobile number
validation</title>
</head>
<body>
<h2>Mobile number
validation</h2>
<form action="#">
<label
for="mobile"> Enter mobile No. :</label>
<input
type="number" name="number" id="mobile">
<input
type="submit" value="Check" name="submit">
</form>
</body>
</html>
<?php
if(isset($_GET['submit'])) {
$num = $_GET['number'];
$pattern_num =
"/^[0-9]{10}$/";
if(preg_match($pattern_num,
$num)) {
echo "$num is valid
<br>";
}
else if($num <= 10 || $num
> 10){
echo "Number must be 10
digit !! <br>";
echo "Not valid !!
<br>";
}
}
?>
===================================================================
SET A:
1. Write a PHP script to accept font name, background color, and welcome
message on 1st page. Display the welcome message with the given font and
background color on the next
page.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
<style>
.container {
width : 500px;
margin: 1rem auto;
border : 1px solid;
border-radius: 8px;
box-shadow: 2px 2px 6px
rgba(0,0,0,0.3);
padding: 2rem;
/* font-family: Arial,
Helvetica, sans-serif; */
}
label {
display: block;
font-size: 16px;
font-weight: 500;
}
</style>
</head>
<body>
<div
class="container">
<h1>Enter Details
</h1>
<form
action="01_text_color.php" method="post">
<label
for="font_name">Font Name :</label>
<input
type="text" name="font_name" id="font_name">
<label
for="back_color">background color : </label>
<input
type="color" name="back_color"
id="back_color">
<label
for="text_msg">Enter text msg :</label>
<input
type="text" name="text_msg" id="text_msg">
<input
type="submit" value="Send">
</div>
</form>
</body>
</html>
--------------------------
PHP File :
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
$font_name = $_POST['font_name'];
$bg_color = $_POST['back_color'];
$text_msg = $_POST['text_msg'];
echo "<html> <body
style='background: $bg_color';>";
echo "<h1
style='font-family:$font_name';>";
echo $text_msg;
echo "</h1>";
echo "</body>
</html>";
}
?>
====================================================================
2. Write a PHP program to accept name, address, pincode, gender
information. If any field is blank display error messages “all fields are
required”.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>Enter your
Details</h1>
<form action="#"
method="POST">
<label
for="name">Name :</label>
<input
type="text" name="name" id="name"
class="input">
<label
for="add">Address :</label>
<textarea
name="add" id="add" cols="30" rows="3"
class="input"></textarea>
<label
for="pin_code">Pin Code :</label>
<input
type="number" name="pin_code" id="pin_code"
class="input">
<label
for="gender">Gender :</label>
<input type="radio"
name="g" id="male" value="Male" > <label
for="male" class="gender_label">Male</label>
<input
type="radio" name="g" id="female"
value="Female"> <label for="female"
class="gender_label">Female</label>
<input type="submit"
value="Submit" class="btn" name="submit">
</form>
</div>
</body>
</html>
-------------------------------
PHP File :
<?php
error_reporting(0);
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$add = $_POST['add'];
$pin = $_POST['pin_code'];
$gender = $_POST['g'];
if(empty($name) ||
empty($add) || empty($pin) || empty($gender)) {
echo "<script>
alert('All field are required !!') </script>";
} else {
echo "<script>
alert('Form submitted successfully !!') </script>";
}
}
?>
======================================================================
3. Write a PHP script to accept employee details (name, address) and
earning details (basic, DA, HRA). Display employee details and earning details
in the proper format.
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Employee
Details</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.add_label {
display: inline-block;
cursor: pointer;
color: black;
font-size: 14px;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>Employee
details</h1>
<form
action="03_emp_form.php" method="POST">
<label
for="name">Name :</label>
<input
type="text" name="name" id="name"
class="input">
<label
for="add">Address :</label>
<textarea
name="add" id="add" cols="30" rows="3"
class="input"></textarea>
<label>Earning
Details : </label>
<input type="number"
name="salary" id="salary" class="input"
placeholder="Enter basic salary">
<label> Additional
:</label>
<input
type="checkbox" name="check_list[]" value="500"
id="DA"> <label for="DA"
class="add_label">DA ₹500</label>
<input
type="checkbox" name="check_list[]" value="1450"
id="HRA"> <label for="HRA"
class="add_label">HRA ₹1450</label>
<input
type="submit" value="Submit" class="btn"
name="submit">
</form>
</div>
</body>
</html>
--------------------------
PHP File :
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$i = 0;
$name = $_POST['name'];
$add = $_POST['add'];
$salary = $_POST['salary'];
$arr = array();
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as
$ch){
$arr[$i] = $ch;
$i = $i + 1;
}
}
$salary = $salary + $arr[0] +
$arr[1];
echo "<h1> Employee
Details </h1>";
echo "Name : $name
<br>";
echo "Address : $add
<br>";
echo "Total Salary : $salary
<br>";
}
?>
=======================================================
SET B:
1. Write a PHP script to accept customer name and the list of product and
quantity on the first page. On the next page display the name of the customer,
name of the products, rate of the product, quantity, and total price in table
format.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Employee
Details</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.add_label {
display: inline-block;
cursor: pointer;
color: black;
font-size: 14px;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
margin-top: 1rem;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>Order Now
</h1>
<form
action="01_customer_display.php" method="POST">
<label
for="name">Customer Name :</label>
<input
type="text" name="name" id="name"
class="input">
<label>Select
products :</label>
<select
name="product" id="product" class="input">
<option
value="">Your products</option>
<option
value="Mouse">Mouse</option>
<option
value="Keyboard">Keyboard</option>
<option
value="Monitor">Monitor</option>
<option
value="SSD">SSD 256GB</option>
</select>
<label
for="quantity">Quantity :</label>
<input
type="number" name="quantity" id="quantity" class="input">
<input
type="submit" value="Order Now" class="btn"
name="submit">
</form>
</div>
</body>
</html>
---------------------------------
PHP File :
<?php
if(isset($_POST['submit'])) {
$cus_name = $_POST['name'];
$product = $_POST['product'];
$quantity = $_POST['quantity'];
switch ($product) {
case 'Mouse':
$rate = 250;
$total_prize = $rate *
$quantity;
break;
case 'Keyboard':
$rate = 350;
$total_prize = $rate *
$quantity;
break;
case 'Monitor':
$rate = 3000;
$total_prize = $rate *
$quantity;
break;
case 'SSD':
$rate = 1400;
$total_prize = $rate * $quantity;
break;
default:
echo "Something
Wrong!!<br>";
break;
}
echo "<h2> Products
Details </h2>";
echo "<table
border='1'> <tr>
<th>Customer Name
</th>
<th>Product</th>
<th>Rate</th>
<th>Quantity</th>
<th>Total
Prize</th> </tr> <tr> <td>".
$cus_name .
"</td><td>".
$product. "</td>
<td>".
$rate . "</td> <td>".
$quantity. "</td>
<td>".
$total_prize.
"</td> </tr>";
echo "</table>";
}
?>
=========================================================
2. Write HTML code to design multiple choice question paper for PHP subject.
Display question wise marks and total marks received by the student in table
format.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Student
form</title>
<style>
.container {
box-sizing: border-box;
width: 600px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
margin-top: 1rem;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
form {
display: flex;
flex-direction: column;
}
form .form-item {
margin-bottom: .5rem;
}
form .form-item .field_tag {
display: inline-block;
font-size: 16px ;
font-weight: 600;
color: gray;
margin-bottom: 0.5rem;
}
form .form-item .que p{
font-size: 16px;
font-weight: bold;
color: #6f6d6d;
}
form .form-item .que .option
{
display: flex;
align-items: center;
flex-direction: row;
margin: .8rem 0;
margin-left: 1.5rem;
}
form .form-item .que .option
span {
font-size: 16px;
}
label {
cursor: pointer;
}
</style>
</head>
<body>
<div
class="container">
<h1>Obj question
</h1>
<form
action="02_student_form.php" method="POST">
<div
class="form-item">
<span
class="field_tag">Student Name :</span>
<input
type="text" name="name" id="name"
class="input">
</div>
<div class="form-item">
<h3>PHP MCQ
:</h3>
</div>
<div
class="form-item">
<div
class="que">
<p>1. PHP
stands for -</p>
<div
class="option">
<span>a. </span>
<input type="radio" name="que1" id="op1"
value="Hypertext Preprocessor"> <label
for="op1">Hypertext Preprocessor</label>
</div>
<div
class="option">
<span>b. </span> <input type="radio"
name="que1" id="op2" value="Pretext Hypertext
Preprocessor"> <label for="op2">Pretext Hypertext
Preprocessor</label>
</div>
<div
class="option">
<span>c. </span> <input type="radio"
name="que1" id="op3" value="Personal Home
Processor"> <label for="op3">Personal Home
Processor</label>
</div>
<div
class="option">
<span>d. </span> <input type="radio"
name="que1" id="op4" value="None of the
above"> <label for="op4">None of the
above</label>
</div>
</div>
</div>
<div
class="form-item">
<div
class="que">
<p>2. Who
is known as the father of PHP?</p>
<div
class="option">
<span>a. </span> <input type="radio"
name="que2" id="op21" value="Drek Kolkevi">
<label for="op21">Drek Kolkevi</label>
</div>
<div
class="option">
<span>b. </span> <input type="radio"
name="que2" id="op22" value="List Barely">
<label for="op22">List Barely</label>
</div>
<div class="option">
<span>c. </span> <input type="radio"
name="que2" id="op23" value="Rasmus Lerdrof">
<label for="op23">Rasmus Lerdrof</label>
</div>
<div
class="option">
<span>d.
</span> <input type="radio" name="que2"
id="op24" value="None of the above"> <label
for="op24">None of the above</label>
</div>
</div>
</div>
<div
class="form-item">
<div
class="que">
<p>3.
Variable name in PHP starts with -</p>
<div
class="option">
<span>a. </span> <input type="radio"
name="que3" id="op31" value="! (Exclamation)">
<label for="op31">! (Exclamation)</label>
</div>
<div
class="option">
<span>b. </span> <input type="radio"
name="que3" id="op32" value="$ (Dollar)">
<label for="op32">$ (Dollar)</label>
</div>
<div
class="option">
<span>c. </span> <input type="radio"
name="que3" id="op33" value="&
(Ampersand)"> <label for="op33">&
(Ampersand)</label>
</div>
<div
class="option">
<span>d. </span> <input type="radio"
name="que3" id="op34" value="# (Hash)">
<label for="op34"># (Hash)</label>
</div>
</div>
</div>
<div
class="form-item">
<div
class="que">
<p>4. Which
of the following is not a variable scope in PHP?</p>
<div
class="option">
<span>a. </span> <input type="radio"
name="que4" id="op41" value="Extern">
<label for="op41">Extern</label>
</div>
<div
class="option">
<span>b. </span> <input type="radio"
name="que4" id="op42" value="Local"> <label
for="op42">Local</label>
</div>
<div
class="option">
<span>c. </span> <input type="radio"
name="que4" id="op43" value="Static">
<label for="op43">Static</label>
</div>
<div
class="option">
<span>d. </span> <input type="radio"
name="que4" id="op44" value="Global">
<label for="op44">Global</label>
</div>
</div>
</div>
<div
class="form-item">
<div
class="que">
<p>5. Which
of the following is used to display the output in PHP?</p>
<div
class="option">
<span>a. </span> <input type="radio"
name="que5" id="op51" value="echo"> <label
for="op51">echo</label>
</div>
<div
class="option">
<span>b. </span> <input type="radio"
name="que5" id="op52" value="write"> <label
for="op52">write</label>
</div>
<div class="option">
<span>c. </span> <input type="radio"
name="que5" id="op53" value="print"> <label
for="op53">print</label>
</div>
<div
class="option">
<span>d. </span> <input type="radio"
name="que5" id="op54" value="Both (a) and
(c)"> <label for="op54">Both (a) and (c)</label>
</div>
</div>
</div>
<input
type="submit" value="Submit" class="btn"
name="submit">
</form>
</div>
</body>
</html>
----------------------------------
PHP File :
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$true_answer =
array("Hypertext Preprocessor", "Rasmus Lerdrof", "$
(Dollar)", "Extern", "Both (a) and (c)");
$stud_name = $_POST['name'];
$get_answers = array();
$marks = 0;
$get_answers[0] = $_POST['que1'];
$get_answers[1] = $_POST['que2'];
$get_answers[2] = $_POST['que3'];
$get_answers[3] = $_POST['que4'];
$get_answers[4] = $_POST['que5'];
foreach($get_answers as $value) {
if(in_array($value,
$true_answer)) {
$marks = $marks + 1;
}
}
echo "<h2> Student
Details </h2>";
echo "<table
border='1'> <tr>
<th>Student Name
</th>
<th>Correct
Ans</th>
<th>Total
Marks</th>
</tr> <tr>
<td>".
$stud_name .
"</td><td>".
$marks. "</td>
<td>".
$marks . "</td>
</tr>";
}
?>
==========================================================================
3. Write a PHP script to accept student name and list of programming
languages (using drop down box) and display it on the next page in the proper
format.
HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Student
language</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.add_label {
display: inline-block;
cursor: pointer;
color: black;
font-size: 14px;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
margin-top: 1rem;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>Student Info
</h1>
<form
action="03_drop_box_student.php" method="POST">
<label
for="name">Student Name :</label>
<input
type="text" name="name" id="name"
class="input">
<label>Select
programming language :</label>
<select
name="language[]" id="language" class="input"
multiple>
<option
value="Python">Python</option>
<option
value="JavaScript">JavaScript</option>
<option
value="Java">Java</option>
<option
value="PHP">PHP</option>
<option
value="C++">C++</option>
</select>
<input
type="submit" value="Submit" class="btn"
name="submit">
</form>
</div>
</body>
</html>
-------------------------
PHP File :
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$stud_name = $_POST['name'];
$languages = $_POST['language'];
$lan = " ";
foreach ($languages as $x) {
$lan .= $x.", ";
}
echo "<h2> Student
Details </h2>";
echo "<table border='1'>
<tr>
<th>Student Name
</th>
<th>Programming
languages</th> </tr>
<tr>
<td>".$stud_name ."</td><td>".
$lan . "</td>
</tr>";
}
?>
=======================================================================
4. Write a PHP script to accept user name, email address and age. If data
entered by the user is valid then display it on the next page otherwise display
the appropriate message(use filter_var())
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Validation</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.add_label {
display: inline-block;
cursor: pointer;
color: black;
font-size: 14px;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
margin-top: 1rem;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>login </h1>
<form
action="04_validation.php" method="POST">
<label
for="name">User Name :</label>
<input
type="text" name="name" id="name"
class="input" required>
<label
for="email">E-mail :</label>
<input
type="email" name="email" id="email"
class="input" required>
<label
for="age">Age :</label>
<input type="number"
name="age" id="age" class="input" required>
<input
type="submit" value="Submit" class="btn"
name="submit">
</form>
</div>
</body>
</html>
------------------------------
PHP File :
<?php
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$user_pattern = "/^[A-Za-z\-
]+$/";
$user_val =
preg_match($user_pattern, $user);
$email_val = filter_var($email,
FILTER_VALIDATE_EMAIL);
if($user_val &&
$email_val && $age > 0 && $age < 120) {
echo "<script>
alert('Form submitted successfully !!'); </script>";
?>
<meta
http-equiv="refresh" content = "0;04_validation.html">
<?php
} else {
echo "<script>
alert('Check the your data !!'); </script>";
}
}
?>
==========================================================
SET C:
1. A web application that takes name and age from an HTML page. If the age
is less than 18, it should send a page with “Hello <name>, you are not
authorized to visit the site” message, where <name> should be replaced
with the entered name. Otherwise, it should send a “Welcome <name> to
this site” message.
Main HTML File :
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Main
file</title>
<style>
.container {
box-sizing: border-box;
width: 400px;
margin: 2rem auto;
border: 1px solid;
border-radius: 8px;
box-shadow: 3px 3px 10px
rgba(0, 0, 0, 0.2);
padding: 1rem;
font-family: Arial,
Helvetica, sans-serif;
}
label {
display: block;
font-size: 1rem;
padding-bottom: .5rem;
font-weight: 500;
color: gray;
}
.gender_label {
display: inline-block;
cursor: pointer;
}
.add_label {
display: inline-block;
cursor: pointer;
color: black;
font-size: 14px;
}
.container .input {
width: 100%;
box-sizing: border-box;
border: 1px solid
lightblue;
outline: none;
padding: .5rem 0;
padding-left: 1rem;
margin-bottom: .6rem;
}
.btn {
display: block;
margin: .6rem 0;
margin-top: 1rem;
padding: .5rem .8rem;
font-size: 1rem;
font-weight: 600;
background-color:
blueviolet;
color: #fff;
cursor: pointer;
border: none;
outline: none;
border-radius: 6px;
transition: .2s linear;
}
.btn:hover {
color: black;
background-color:
lightblue;
}
</style>
</head>
<body>
<div
class="container">
<h1>Enter Details
</h1>
<form
action="02_check_page.php" method="POST">
<label
for="name">Name :</label>
<input
type="text" name="name" id="name"
class="input" required>
<label
for="age">Age :</label>
<input
type="number" name="age" id="age"
class="input" required>
<input type="submit"
value="Send" class="btn" name="submit">
</form>
</div>
</body>
</html>
------------------------------
PHP File 1. (check the user age is > 18 move the next page)
<?php
if(isset($_POST['submit'])) {
$user = $_POST['name'];
$age = $_POST['age'];
if($age < 18) {
echo "You not
application for visiting site !!";
} else {
header("location:03_welcome.php?n=$user");
}
}
?>
-------------------------------------
PHP File 2 : (Welcome page)
<?php
$name = $_GET['n'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta
charset="UTF-8">
<meta
name="viewport" content="width=device-width,
initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo "<h1> Welcome
$name </h1>";
?>
</body>
</html>
=====================================================================
0 Comments