0% found this document useful (0 votes)
46 views47 pages

PHP Aayu

The document contains a series of PHP programming exercises designed for students, including tasks such as calculating factorials, checking odd/even numbers, string manipulations, and basic arithmetic operations. Each exercise includes HTML input forms, PHP code snippets, and expected outputs to guide learners through practical coding applications. The exercises cover a range of topics from basic syntax to more complex functions and array manipulations.

Uploaded by

vatsv3temp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views47 pages

PHP Aayu

The document contains a series of PHP programming exercises designed for students, including tasks such as calculating factorials, checking odd/even numbers, string manipulations, and basic arithmetic operations. Each exercise includes HTML input forms, PHP code snippets, and expected outputs to guide learners through practical coding applications. The exercises cover a range of topics from basic syntax to more complex functions and array manipulations.

Uploaded by

vatsv3temp
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/ 47

PHP

PROGRAMMING-I
Name: KOTADIYA AYUSHI KANJIBHAI
Roll No: CS22037

ATMANAND SARASWATI SCIENCE COLLEGE


KAPODRA, VARACHHA ROAD, SURAT – 395006.
PHP PROGRAMMING-I| CS22052

1. Write a PHP program that find out factorial of entered


number.
INPUT:
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Factorial Calculation</title>

</head>

<body>

<?php

function factorial($n)

if($n < 0) {

return "Undefined for negative numbers";

$fact = 1;

for($i = $n; $i > 0; $i--)

$fact *= $i;

return $fact;

$n = 10;

PAGE 1 OF 46
PHP PROGRAMMING-I| CS22052

$result = factorial($n);

echo "Factorial of " . $n . " is " . $result;

?>

</body>

</html>

OUTPUT:

2. Display Odd and Even Value Of Given Number.


INPUT:
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Odd or Even Number Check</title>

</head>

<body>

<form method="post" >

PAGE 2 OF 46
PHP PROGRAMMING-I| CS22052

<label>Enter the integer number : </label><input type="number" name="number1"


/><br>

<button type="submit" name="submit" >Submit</button>

</form>

<?php

if(isset($_POST['submit']))

$num=$_POST['number1'];

if($num%2==0)

echo "The given number ".$num." is even.";

else

echo "The given number ".$num." is odd.";

?>

</body>

</html>

PAGE 3 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

3. Find String Length of given String ,String Position and


String Reverse.
INPUT:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>String Operations</title>

</head>

<body>

<?php

$string="Hello,It's a new day.";

$search="new";

echo "The given string is : ".$string."<br><br>";

echo "Length of the string is ".strlen($string)."<br><br>";

echo "Reverse string is : ".strrev($string)."<br><br>";

PAGE 4 OF 46
PHP PROGRAMMING-I| CS22052

echo "Position of the \" {$search} \" word in string is ".strpos($string,$search);

?>

</body>

</html>

OUTPUT:

4. Write a PHP script to get the last three characters of a


string. Sample String : '[email protected]' Expected
Output : 'com'.
INPUT:
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

PAGE 5 OF 46
PHP PROGRAMMING-I| CS22052

<title>Document</title>

</head>

<body>

<?php

$string="[email protected]";

function endchar($string)

for($i=strlen($string)-3;$i<strlen($string);$i++)

echo $string[$i];

echo "The given string is : ".$string."<br>"."The last three character are : ";

echo endchar($string);

?>

</body>

</html>

OUTPUT:

PAGE 6 OF 46
PHP PROGRAMMING-I| CS22052

5. Write a PHP script to replace the first 'the' of the


following string with 'That'. Sample date : 'the quick
brown fox jumps over the lazy dog.' Expected Result :
That quick brown fox jumps over the lazy dog.
INPUT:

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

$string="the quick brown fox jump over the lazy dog.";

$result=str_replace("the","that",$string);

echo "The given string is : <br>".$string;

echo "<br>The replace string is : <br>";

echo $result;

?>

</body>

</html>

PAGE 7 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

6. Write a PHP script to get the first word of a sentence.


Original String : 'The quick brown fox'
Expected Output : 'The'.
INPUT:
<!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

$string="The world is beautiful!"."<br>";

echo "The given string : ".$string."<br>";

echo "\nThe first word of the string is -> ".strtok($string," ");

?>

PAGE 8 OF 46
PHP PROGRAMMING-I| CS22052

</body>

</html>

OUTPUT:

7. Write a PHP script to find the maximum and minimum


marks from the following set of arrays.
$marks1 = array(360,310,310,330,313,375,456,111,256);
$marks2 = array(350,340,356,330,321);
$marks3 = array(630,340,570,635,434,255,298);
Expected Output :
Maximum marks : 635
Minimum marks : 111.
INPUT:
<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

PAGE 9 OF 46
PHP PROGRAMMING-I| CS22052

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Document</title>

</head>

<body>

<?php

$marks1=array(360,310,310,330,313,375,456,111,256);

$marks2=array(350,340,356,330,321);

$marks3=array(630,340,570,635,434,255,298);

$allMarks = array_merge($marks1, $marks2, $marks3);

function max_min($allMarks)

echo "Maximum : ".max($allMarks)."<br>";

echo "Minimum : ".min($allMarks)."<br>";

echo max_min($allMarks)."<br>";

?>

</body>

</html>

OUTPUT:

PAGE 10 OF 46
PHP PROGRAMMING-I| CS22052

8. Write a PHP function to get random number between


40 to 60.
INPUT:

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

$n=5;

echo "Random numbers between 40 and 60 : ";

for($i=0;$i<$n;$i++)

echo rand(40,60).",";

?>

</body>

</html>

PAGE 11 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

9. Write a PHP function to following number convert


negative number to positive number. A = -10 B = -20
ANS = A – B.
INPUT:

<!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
$a=-10;
$b=-20;
echo "The value of a is ".$a."<br>";
echo "The value of b is ".$b."<br>";
echo "The value of a-b is ".abs($a)-abs($b);
?>
</body>
</html>

OUTPUT:

PAGE 12 OF 46
PHP PROGRAMMING-I| CS22052

10. Write a PHP function to get ceil value of following


number = 0.70.
INPUT:

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

$n=0.74;

echo "The value of n is ".$n."<br>";

echo "The cell value of n is ".ceil($n);

?>

</body>

</html>

PAGE 13 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

11. Create a script that displays 1-2-3-4-5-6-7-8-9-10 on one


line .there will be no hyphen(-) at starting and ending
position.
INPUT:

<!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
for($n=1;$n<11;$n+=1){
if($n==10)
{
echo $n." ";

PAGE 14 OF 46
PHP PROGRAMMING-I| CS22052

break;
}
echo " ".$n."-";

}
?>
</body>
</html>

OUTPUT:

12. Write a PHP Script to display ODD numbers from 1 to 20


using any loop.
INPUT:

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

PAGE 15 OF 46
PHP PROGRAMMING-I| CS22052

<?php
echo "Odd numbers from 1 to 20 :\n";
for($n=1;$n<20;$n+=2)

{
echo " ".$n;
}
?>
</body>

</html>

OUTPUT:

13. Create a script using a for loop to add all the integers
between 0 and 30 and display the total.
INPUT:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">


<title>Document</title>

PAGE 16 OF 46
PHP PROGRAMMING-I| CS22052

</head>
<body>
<?php

$sum=0;
for($i=0;$i<30;$i++)
{
$sum+=$i;
}

echo "The sum of 0 to 30 integers is ".$sum;


?>
</body>
</html>

OUTPUT:

PAGE 17 OF 46
PHP PROGRAMMING-I| CS22052

14. Create a script to construct the following pattern, using


nested for loop.
*
**
***
****
*****
INPUT:

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

for ($i = 0; $i < 5; $i++)

for ($j = 0; $j <= $i; $j++)

echo "* ";

echo "<br>";

PAGE 18 OF 46
PHP PROGRAMMING-I| CS22052

?>

</body>

</html>

OUTPUT:

15. Implement a PHP Script to:


• Create an array named subjects.
• Add the subjects “PHP”,”phython”,”java” into the
array.
• Create multidimensional array named subjects with
marks.

PAGE 19 OF 46
PHP PROGRAMMING-I| CS22052

INPUT:
<!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
$subject=array("PHP","JAVA","PYTHON");
echo "<h3>Accessing through for loop </h3>";
echo "Subjects = [";

for($n=0;$n<3;$n+=1)
{
echo " ".$subject[$n].",";
}
echo " ]<br><hr>";

echo "<h3>Accessing through index </h3><br>";


echo "Subject : ".$subject[0]."<br>";
echo "Subject : ".$subject[1]."<br>";
echo "Subject : ".$subject[2]."<br>";
$mark=array(

array("PHP",100,95),
array("JAVA",100,85),
array("PYTHON",100,80)
);
echo "<hr>";
echo "<h3>Accessing through index </h3><br>";

PAGE 20 OF 46
PHP PROGRAMMING-I| CS22052

echo "Subject : ".$mark[0][0]." , Total : ".$mark[0][1]." , Obtained :


".$mark[0][2]."<br>";
echo "Subject : ".$mark[1][0]." , Total : ".$mark[1][1]." , Obtained :
".$mark[1][2]."<br>";
echo "Subject : ".$mark[2][0]." , Total : ".$mark[2][1]." , Obtained :
".$mark[2][2]."<br>";
?>
</body>

</html>

OUTPUT:

PAGE 21 OF 46
PHP PROGRAMMING-I| CS22052

16. Write a PHP program to calculate area of rectangle.


INPUT:

<!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>
<form method="post">

<label>Enter the length : </label><input type="number" name="length"/><br>


<label>Enter the Breadth : </label><input type="number" name="breadth"/><Br>
<input type="submit" name="submit"/><br>
</form>

<?php
if(isset($_POST["submit"]))
{
$length = $_POST['length'];
$breadth = $_POST['breadth'];

echo "\n The length of rectangle is ".$length."<br>";


echo "\n The Breadth of rectangle is ".$breadth."<br>";
echo "\n The Area of rectangle is ".($length*$breadth)."<br>";
}
?>
</body>

PAGE 22 OF 46
PHP PROGRAMMING-I| CS22052

</html>

OUTPUT:

17. Write a PHP program to arithmetic operator in two


number.
INPUT:

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

PAGE 23 OF 46
PHP PROGRAMMING-I| CS22052

<form method="post" >


<label>Number 1 : </label><input type="number" name="number1" /><br>
<label>Number 2 : </label><input type="number" name="number2"/><br>

<button type="submit" name="Addition" >Addition</button>


<button type="submit" name="Subtract">Subtraction</button>
<button type="submit" name="Multiply">Multiply</button>
<button type="submit" name="Division">Division</button>
<button type="submit" name="Modulus">Modulus</button>

</form>
<?php
if(isset($_POST['Addition']))
{
$num1=$_POST['number1'];

$num2=$_POST['number2'];

echo "<br>The Addition of {$num1} and {$num2} is ".$num1+$num2;


}
if(isset($_POST['Subtract']))

{
$num1=$_POST['number1'];
$num2=$_POST['number2'];

echo "<br>The Subtraction of {$num1} and {$num2} is ".$num1-$num2;

}
if(isset($_POST['Multiply']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];

PAGE 24 OF 46
PHP PROGRAMMING-I| CS22052

echo "<br>The Multiplication of {$num1} and {$num2} is ".$num1*$num2;


}
if(isset($_POST['Division']))

{
$num1=$_POST['number1'];
$num2=$_POST['number2'];

echo "<br>The Division of {$num1} and {$num2} is ".$num1/$num2;

}
if(isset($_POST['Modulus']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];

echo "<br>The Remainder of {$num1} divided by {$num2} is ".$num1%$num2;


}
?>
</body>

</html>

OUTPUT:

PAGE 25 OF 46
PHP PROGRAMMING-I| CS22052

18. Write a PHP class called 'Vehicle' with properties like


'brand', 'model', and 'year'. Implement a method to
display the vehicle details.
INPUT:
<!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>

PAGE 26 OF 46
PHP PROGRAMMING-I| CS22052

<body>
<?php
class vehicle

{
public String $brand;
public String $model;
public int $year;
public function __construct($brand,$model,$year)

{
$this->brand = $brand;
$this->model = $model;
$this->year = $year;
echo "-----The detail of vehicle-----<br>";

echo "Brand : ".$brand."<br>";


echo "Model : ".$model."<br>";
echo "Year : ".$year."<br>";
}

}
$obj = new vehicle("Honda","YG22",2022);
?>
</body>
</html>

PAGE 27 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

19. Write a PHP program that calculate simple interest.


(Enter values from user).
INPUT:

<!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>
<form method="post" >
<label>Principle Amount : </label><input type="number" name="principle"
/><br>
<label>Rate of Interest : </label><input type="number" name="rate"/><br>
<label>Number of months : </label><input type="number" name="month"/><br>

PAGE 28 OF 46
PHP PROGRAMMING-I| CS22052

<input type="submit" name="submit"/>


</form>
<?php

if(isset($_POST['submit']))
{
$principle=$_POST['principle'];
$rate=$_POST['rate'];

$months=$_POST['month'];

$si=($principle*$rate*$months)/100;

echo "The simple interest per month is ".$si;

}
?>
</body>

</html>

OUTPUT:

PAGE 29 OF 46
PHP PROGRAMMING-I| CS22052

20. Write a PHP class called 'Student' with properties like


'name', 'age', and 'grade'. Implement a method to
display student information.
INPUT:
<?php

class student
{
public $name;
public $age;
public $grade;

function setdetail($name,$age,$grade)
{
$this->name=$name;
$this->age=$age;

$this->grade=$grade;
}
function getdetail()
{

PAGE 30 OF 46
PHP PROGRAMMING-I| CS22052

echo "----------Student Detail-------<br>";


echo "\tName : ".$this->name."<br>";
echo "\tAge : ".$this->age."<br>";

echo "\tGrade : ".$this->grade."<br>";

}
$obj= new student();
$obj->setdetail("Suraj",23,300);
$obj->getdetail();

?>

OUTPUT:

21. Write a PHP program that accept two different values


from user and perform addition.
INPUT:

PAGE 31 OF 46
PHP PROGRAMMING-I| CS22052

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

<form method="post" >


<label>Number 1 : </label><input type="number" name="number1" /><br>
<label>Number 2 : </label><input type="number" name="number2"/><br>
<input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit']))
{
$num1=$_POST['number1'];
$num2=$_POST['number2'];

echo "<br>The addition of {$num1} and {$num2} is ".$num1+$num2;


}
?>
</body>

</html>

OUTPUT:

PAGE 32 OF 46
PHP PROGRAMMING-I| CS22052

22. Make a PHP College Management Website Which


Contain Following Pages with database connectivity.
1.Home.PHP 2.AboutUS.PHP 3. Course.php .
INPUT:

Config.php

<?php
$servername="localhost";
$username= "root";
$password="";

PAGE 33 OF 46
PHP PROGRAMMING-I| CS22052

$database = 'college';

$conn= new mysqli($servername,$username,$password,$database);

//if(!$conn)
//{
// echo "Connection failed";
//}
if ($conn -> connect_errno) {

echo "Failed to connect to MySQL: " . $conn -> connect_error;


exit();
}
?>

Home.php
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Homepage</title>
</head>
<body>

<h1>Welcome to Our College Management System</h1>


<p>This is the home page where you can explore our college's courses.</p>

<nav>
<a href="Home.php">Home</a> |
<a href="AboutUs.php">About Us</a> |

PAGE 34 OF 46
PHP PROGRAMMING-I| CS22052

<a href="Course.php">Courses</a>
</nav>

<p>Explore our academic programs and faculty to know more about what we
offer.</p>

<span>Enroll yourself a course :<a style="color:blue;" href="course.php">CLick


here</a> </span>
</body>
</html>

aboutus.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>AboutUs</title>
</head>
<body>
<h1>About Us</h1>
<nav>

<a href="Home.php">Home</a> |
<a href="AboutUs.php">About Us</a> |
<a href="Course.php">Courses</a>
</nav>

<p>Founded in 2000, we aim to provide quality education to students across the


country.</p>

</body>

PAGE 35 OF 46
PHP PROGRAMMING-I| CS22052

</html>

course.php

<?php
// Include the database connection file
include('config.php');

function result()

{
global $conn; // Use the connection from config.php

if (isset($_POST['submit'])) {
// Retrieve form inputs

$name = $_POST['name'];
$rollno = $_POST['rollno'];
$contact = $_POST['contact'];
$course = $_POST['course'];
if($course=='none')

{
echo "Please select the course.";
}
else{

// Prepare and execute the INSERT query to save data in the 'students' table
$query = "INSERT INTO `course`(`name`, `rollno`, `contact`, `course`) VALUES
('$name','$rollno','$contact','$course')";

PAGE 36 OF 46
PHP PROGRAMMING-I| CS22052

$result = mysqli_query($conn, $query);

// Check if insertion was successful

if ($result) {
return "Registration Successful!<br>" .
"Name: $name<br>" .
"Roll No: $rollno<br>" .
"Contact: $contact<br>" .

"Course: $course";
} else {
return "Error Occurred. Registration Failed.";
}
}

}
else {
return "Please fill out the form to register.";
}

}
?>

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Course Registration</title>
</head>

PAGE 37 OF 46
PHP PROGRAMMING-I| CS22052

<body align="center">
<nav>

<a href="Home.php">Home</a> |
<a href="AboutUs.php">About Us</a> |
<a href="Course.php">Courses</a>
</nav>
<h1 align="center">Course Registration</h1>

<form method="post" action="" align="center">


<div>
<label>Student Name: </label>
<input type="text" name="name" required /><br>

<label>Student Roll No: </label>


<input type="number" name="rollno" required /><br>

<label>Student Contact: </label>


<input type="number" name="contact" required /><br>

<label>Course: </label>
<select name="course" required>
<option value="none">none</option>
<option value="Maths">Maths</option>

<option value="Microbiology">Microbiology</option>
<option value="Chemistry">Chemistry</option>
</select><br>

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


</div>

PAGE 38 OF 46
PHP PROGRAMMING-I| CS22052

</form>

<label id="result">

<?php echo result(); ?>


</label>
</body>

</html>

OUTPUT:

PAGE 39 OF 46
PHP PROGRAMMING-I| CS22052

23. Write a PHP Script Which Change a Background Color on


Selection Of Color From DropDown.
INPUT:
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Change Background Color</title>
</head>
<body style="background-color: <?php echo isset($_POST['color']) ? $_POST['color'] :
'white'; ?>;">

PAGE 40 OF 46
PHP PROGRAMMING-I| CS22052

<form method="post" action="">


<label for="color">Choose a background color:</label>

<select name="color" id="color">


<option value="white">White</option>
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>

<option value="yellow">Yellow</option>
<option value="pink">Pink</option>
</select>
<input type="submit" value="Change Color">
</form>

<?php
if (isset($_POST['Change Color']))
{
$color = $_POST['color'];
echo "<style> .*{bgcolor :$color;}</style>";

}
?>
</body>
</html>

OUTPUT:

PAGE 41 OF 46
PHP PROGRAMMING-I| CS22052

24. Write a PHP Form to display student data using form


Post Method with Proper Validation. 1.First Name 2.Last
Name 3.Gender 4.Address.5.Email. 6.D.O.B.
INPUT:
<!DOCTYPE HTML>
<html>

<head>
<style>

.error {
color: #FF0000;
}
</style>
</head>

<body>
<?php
// define variables and set to empty values
$fnameErr = $lnameErr = $genderErr = $addressErr = $emailErr = $dobErr = "";

$fname = $lname = $gender = $address = $email = $dob = "";


if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {

PAGE 42 OF 46
PHP PROGRAMMING-I| CS22052

$fnameErr = "First Name is required";


} else {
$fname = test_input($_POST["fname"]);

}
if (empty($_POST["lname"])) {
$lnameErr = "Last Name is required";
} else {
$lname = test_input($_POST["lname"]);

}
if (empty($_POST["gender"])) {
$genderErr = "Gender is required";
} else {
$gender = test_input($_POST["gender"]);

}
if (empty($_POST["address"])) {
$addressErr = "Address is required";
} else {
$address = test_input($_POST["address"]);

}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);

}
if (empty($_POST["dob"])) {
$dobErr = "Date of Birth is required";
} else {
$dob = test_input($_POST["dob"]);
}

PAGE 43 OF 46
PHP PROGRAMMING-I| CS22052

}
function test_input($data)
{

$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>
<h2>PHP Form Validation Example</h2>
<p><span class="error">* required field</span></p>
<form method="post" action="<?php echo
htmlspecialchars($_SERVER["PHP_SELF"]); ?>">

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


<span class="error">* <?php echo $fnameErr; ?></span>
<br><br>
Last Name: <input type="text" name="lname">
<span class="error">* <?php echo $lnameErr; ?></span>

<br><br>
Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="other">Other

<span class="error">* <?php echo $genderErr; ?></span>


<br><br>
Address: <textarea name="address" rows="5" cols="40"></textarea>
<span class="error">* <?php echo $addressErr; ?></span>
<br><br>
E-mail: <input type="text" name="email">

PAGE 44 OF 46
PHP PROGRAMMING-I| CS22052

<span class="error">* <?php echo $emailErr; ?></span>


<br><br>
Date Of Birth: <input type="date" name="dob">

<span class="error">* <?php echo $dobErr; ?></span>


<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php

echo "<h2>Your Input:</h2>";


echo $fname;
echo "<br>";
echo $lname;
echo "<br>";

echo $gender;
echo "<br>";
echo $address;
echo "<br>";
echo $email;

echo "<br>";
echo $dob;
?>
</body>

</html>

PAGE 45 OF 46
PHP PROGRAMMING-I| CS22052

OUTPUT:

PAGE 46 OF 46

You might also like