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

Php Program1to8

The document contains several PHP programs demonstrating various functionalities such as displaying biodata, reversing a string, checking number types (perfect, abundant, deficient), file uploads, cookie management, and array operations. It also includes a multiplication table generator using PHP and AJAX. Each program is structured with HTML forms for user input and PHP scripts for processing and displaying results.
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)
3 views

Php Program1to8

The document contains several PHP programs demonstrating various functionalities such as displaying biodata, reversing a string, checking number types (perfect, abundant, deficient), file uploads, cookie management, and array operations. It also includes a multiplication table generator using PHP and AJAX. Each program is structured with HTML forms for user input and PHP scripts for processing and displaying results.
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/ 11

PHP

PROGRAMS

1. Create a PHP program to display the bio data of the person by reading the personal details using
an HTML page.

Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Biodata</title>
</head>
<body><fieldset><legend>BIODATA</legend>
<form method="POST">
<table>
<tr><td> Name </td><td>: <input type="text" name="txt_name"/></td></tr>
<tr><td> Date of birth</td><td>: <input type="text" name="txt_date"/></td></tr>
<tr><td> Sex </td><td>: <select name="sex">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</td></tr>
<tr><td> Religion</td><td>: <input type="text" name="txt_rg"/></td></tr>
<tr><td> Cast </td><td>: <input type="text" name="txt_ct"/></td></tr>
<tr><td> Nationality </td><td>: <input type="text" name="txt_nt"/></td></tr>
<tr><td> Father's name </td><td>: <input type="text" name="txt_fn"/></td></tr>
<tr><td> Mother's name </td><td>: <input type="text" name="txt_mn"/></td></tr>
<tr><td> Address</td><td>: <textarea name="txt_adr"></textarea></td></tr>
<tr><td></td><td><input type="submit" value="Show" name="btn_show"/></td></tr></table>
<?php
if(isset($_POST['btn_show']))
{
$name=$_POST['txt_name'];
$db=$_POST['txt_date'];
$sex=$_POST['sex'];
$rg=$_POST['txt_rg'];
$ct=$_POST['txt_ct'];
$nt=$_POST['txt_nt'];
$fn=$_POST['txt_fn'];
$mn=$_POST['txt_mn'];
$adr=$_POST['txt_adr'];
echo "BIODATA<br>";
echo"********<br>";
echo"Name:$name<br>";
echo"Date of birth:$db<br>";
echo"Sex:$sex<br>";
echo"Religion:$rg<br>";
echo"Cast:$ct<br>";
echo"Nationality:$nt<br>";
echo"Father's name:$fn<br>";
echo"mother's name:$mn<br>";
echo"Address:$adr<br>";
}
?>
</fieldset>
</form>
</body>
</html>

2. Write a PHP function to reverse a string.

Program code
<!DOCTYPE HTML>
<html>
<head>
<title>Biodata</title>
</head>
<body><fieldset><legend>Reverse a string</legend>
<form method="POST">
<table>
<tr><td> Enter a string </td><td>: <input type="text" name="txt_str"/></td></tr>
<tr><td></td><td><input type="submit" value="Reverse" name="btn_rv"/></td></tr></table>
<?php
if(isset($_POST['btn_rv']))
{
$str=$_POST['txt_str'];
echo"Reversed strin is:",strrev($str);
}
?>
</fieldset>
</form>
</body>
</html>
3. Write a PHP program to check whether the given number is perfect, abundant or deficient.

Program code

<!DOCTYPE HTML>
<html>
<head>
</head>
<body><fieldset><legend>Perfect or Deficient or Abundant number</legend>
<form method="POST">
<table>
<tr><td> Enter a number </td><td>: <input type="text" name="txt_num"/></td></tr>
<tr><td></td><td><input type="submit" value="Submit" name="btn"/></td></tr></table>
<?php
if(isset($_POST['btn']))
{
$n=$_POST['txt_num'];
$sum=0;
for($i=1;$i<$n;$i++)
{
if($n%$i==0)
$sum=$sum+$i;
}
if($sum==$n)
echo"$n is Perfect
number"; else
if($sum<$n)
echo"$n is Deficient
number"; else
echo"$n is Abundant number";
}
?>
</fieldset>
</form>
</body>
</html>

4. Write a PHP script to upload the file to the server through HTML
Page

upload.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Upload</title>
</head>
<body>
<h2>Upload a File</h2>
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Choose file to upload:</label>
<input type="file" id="file" name="file" required>
<br><br>
<input type="submit" value="Upload File">
</form>
</body>
</ht
ml>

upload.php
<?php
$target_dir = "uploads/";

// Create the uploads directory if it doesn't


existif (!is_dir($target_dir)) {
mkdir($target_dir, 0755, true);
}

// Get the file name and target file path


$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
// Check if the file is a valid
uploadif
(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);if
($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}

// Check if file already


existsif
(file_exists($target_file)
){
echo "Sorry, file already exists.";
$uploadOk = 0;
}

// Check file size (limit to 5MB)


if ($_FILES["file"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}

// Allow certain file formats (optional)


$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
if ($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" &&
$imageFileType != "gif") {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an
errorif ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// If everything is ok, try to upload the file
} else {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
echo "The file " . htmlspecialchars(basename($_FILES["file"]["name"])) . " has been
uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

5. PHP program to store current date-time in a cookie and display the last visited date-time on the
web page upon revisiting the same web pages
Program code
<!DOCTYPE HTML>
<html>
<head>
<title>cookies</title>
</head>
<body>
<?php
$cookie_expiration_time = time() + ( 60 * 60 * 24 * 60);
$cookie_name = "last_visit";

if (isset($_COOKIE[$cookie_name])) {

$last_visit = $_COOKIE[$cookie_name];
echo "Your last visit was on: " . $last_visit . "<br>";
} else {
echo "This is your first visit!<br>";
}

$current_time = date("Y/m/d - H:i:s");


setcookie($cookie_name, $current_time, $cookie_expiration_time);

echo "Current visit time: " . $current_time;


?>
</body>
</html>

6. Write an HTML page to display a list of fruit in a list box. Write a PHP program to display
the selected fruit in a webpage.

Program code

<!DOCTYPE HTML>
<html>
<head>
<title>Fruit list</title>
</head>
<body><fieldset><legend>Fruit list</legend>
<form method="POST">
<table>
<tr><td> Select fruit name</td><td>: <select name="fruit">
<option value="">Select</option>
<option value="Mango">Mango</option>
<option value="Banana">Banana</option>
<option value="Grape">Grape</option>
<option value="Orange">Orange</option>
</td></tr>
<tr><td></td><td><input type="submit" value="Show" name="btn_show"/></td></tr>
</table>
<?php
if(isset($_POST['btn_show']))
{

$fruit=$_POST['fruit'];
echo"Selecter fruit is:$fruit";
}
?>
</fieldset>
</form>
</body>
</html>
7. Write a PHP program to create an array and store 10 names in the array. Do the following
operations.
a. Display the content using for each statement.
b. Display the array in a sorted order.
c. Display the array without the duplicate elements.
d. Remove the last element and display.
e. Display the array in reverse order.
f. Search an element in the given array.

Program code

<!DOCTYPE HTML>
<html>
<head>
<title>Array operations</title>
</head>
<body><fieldset><legend>Array operations</legend>
<form method="POST">
<table>
<tr><td>Display array elements</td><td><input type="submit" value="Submit"
name="btn_disp"/></td></tr>
<tr><td>Sort array elements</td><td><input type="submit" value="Submit"
name="btn_sort"/></td></tr>
<tr><td>Remove duplicate elements</td><td><input type="submit" value="Submit"
name="btn_dup"/></td></tr>
<tr><td>Remove last element</td><td><input type="submit" value="Submit"
name="btn_rm"/></td></tr>
<tr><td>Reverse array elements</td><td><input type="submit" value="Submit"
name="btn_rvs"/></td></tr>
<tr><td>Search an array elements</td><td>: <input type="text"
name="txt_name"/></td><td><input type="submit" value="Submit" name="btn_srch"/></td></tr>
</table>
</fieldset>
</form>
</body>
</html>
<?php
$a=array("Anu","Anju","Arjun","Manju","Meera","Anu","Vinu","Adhi","Anu","Neena");
if(isset($_POST['btn_disp']))
{
echo "Array elements are... <br>";
foreach($a as $n)
echo "$n <br>";
}
if(isset($_POST['btn_sort']))
{
sort($a);
echo "Sorted array elements are... <br>";
foreach($a as $n)
echo "$n <br>";
}

if(isset($_POST['btn_dup']))
{
$s=array_unique($a);
echo "Array without duplicate elements are... <br>"; foreach($s
as $n)
echo "$n <br>";
}
if(isset($_POST['btn_rm']))
{
array_pop($a);
echo "Removing last elements .. <br>";
foreach($a as $n)
echo "$n <br>";
}
if(isset($_POST['btn_rvs']))
{
$s=array_reverse($a);
echo "Array in reverse order... <br>";
foreach($s as $n)
echo "$n <br>";
}
if(isset($_POST['btn_srch']))
{
$s=$_POST['txt_name'];
if(in_array($s,$a))
echo "Element present in the array <br>";
else
echo "Element not present";
}
?>

8. Display multiplication table using PHP and AJAX


Program code

Multiplication.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multiplication Table</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>Multiplication Table Generator</h2>
<form id="multiplicationForm">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" required>
<button type="submit">Generate Table</button>
</form>

<div id="result"></div>

<script>
$(document).ready(function () {
$("#multiplicationForm").on("submit", function (e) {
e.preventDefault(); // Prevent form submission

// Get the input value


var number = $("#number").val();

// Send an AJAX request


$.ajax({
url: 'generate_table.php',
type: 'POST',
data: { number: number },
success: function (response) {
$("#result").html(response); // Display the result in the 'result' div
}
})
}) ;
});;
</script>
</body>
</html>

generate_table.php

<?php
if($_POST)
{
$num = $_POST["number"];

echo nl2br("<h3 style='text-align: center;'>


Multiplication Table of
$num:</h3>
");

for ($i = 1; $i <= 10; $i++) {


echo ("<p style='text-align: center;'>$num"
. " X " . "$i" . " = "
. $num * $i . "</p>
");
}
}
?>

You might also like