0% found this document useful (0 votes)
3 views6 pages

Lab Programs - 11-15

The document contains multiple PHP scripts for handling form inputs using GET and POST methods. It includes examples for submitting user information, checking if a number is prime, accepting string inputs, and performing matrix addition. Each script demonstrates how to process and display submitted data from HTML forms.

Uploaded by

suraiyareeha
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 views6 pages

Lab Programs - 11-15

The document contains multiple PHP scripts for handling form inputs using GET and POST methods. It includes examples for submitting user information, checking if a number is prime, accepting string inputs, and performing matrix addition. Each script demonstrates how to process and display submitted data from HTML forms.

Uploaded by

suraiyareeha
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/ 6

Lab Program 11

Write a PHP script to implement form handling using get method.


<html>
<head>
<title>Form Handling using GET Method</title>
</head>
<body>

<h2>PHP Script to Implement Form Handling Using Get Method: </h2>

<h3>User Information Form</h3>

<form method="get" action="">


Enter your name: <br>
<input type="text" name="name" required><br><br>

Enter your age: <br>


<input type="number" name="age" required><br><br>

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


</form>

<?php
// Check if form data is available in the URL
if (isset($_GET['name']) && isset($_GET['age'])) {
$name = $_GET['name'];
$age = (int)$_GET['age'];

echo "<h3>Submitted Data:</h3>";


echo "Name: " . $name . "<br>";
echo "Age: " . $age;
}
?>

</body>
</html>
Lab Program 12
Write a PHP script to implement form handling using post method.
<html>
<head>
<title>Form Handling using POST Method</title>
</head>
<body>

<h2>PHP Script to Implement Form Handling Using Post Method: </h2>

<h3>User Information Form</h3>

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


Enter your name: <br>
<input type="text" name="name" required><br><br>

Enter your age: <br>


<input type="number" name="age" required><br><br>

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


</form>

<?php
// Check if form data is submitted using POST
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['name']) &&
isset($_POST['age'])) {
$name = $_POST['name']; // prevent XSS
$age = (int)$_POST['age'];

echo "<h3>Submitted Data:</h3>";


echo "Name: " . $name . "<br>";
echo "Age: " . $age;
}
?>

</body>
</html>
Lab Program 13
Write a PHP script that receives form input by the method post to
check the number is prime or not.
<html>
<head>
<title>Prime Number Checker</title>
</head>

<body>
<h2>PHP Script That Receives Form Input by the Method Post to Check the Number
is Prime or Not: </h2>

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


Enter a number: <br>
<input type="number" name="number"><br><br>
<input type="submit" value="Check Prime">
</form>

<?php
// Function to check if number is prime

function IsPrime($n) {
if ($n <= 1) return false; // Numbers <= 1 are NOT prime

for ($i = 2; $i < $n; $i++) {


if ($n % $i == 0) {
return false; // Found a number that divides $n exactly
}
}
return true; // No number divides $n → Prime!
}

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['number'])) {


$num = (int) $_POST['number'];

if (IsPrime($num)) {
echo "<h3>$num is a Prime Number. </h3>";
} else {
echo "<h3>$num is NOT a Prime Number. </h3>";
}
}
?>

</body>
</html>
Lab Program 14
Write a PHP script that receives string as a form input.
<html>
<head>
<title>String Form Input</title>
</head>
<body>

<h2>PHP Script that Accepts String as a Form Input: </h2>

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


Enter your string: <br>
<input type="text" name="string" required><br><br>
<input type="submit" value="Submit">
</form>

<?php
// Check if form is submitted
if ($_POST) {
// Get the string from POST
$string = $_POST['string'];

// Display the input string


echo "<h3>You entered:</h3>";
echo $string;
}
?>

</body>
</html>
Lab Program 15
Write a PHP script to compute addition of two matrices as a form
input.
<!DOCTYPE html>
<html>
<head>
<title>Matrix Addition</title>
</head>
<body>

<h2>PHP script to Compute Addition of Two Matrices as a Form Input</h2>

<form method="post">
<h3>Enter Elements of Matrix A:</h3>
<input type="number" name="a00">
<input type="number" name="a01"><br><br>
<input type="number" name="a10">
<input type="number" name="a11"><br><br>

<h3>Enter Elements of Matrix B:</h3>


<input type="number" name="b00">
<input type="number" name="b01"><br><br>
<input type="number" name="b10">
<input type="number" name="b11"><br><br>

<input type="submit" value="Add Matrices">


</form>

<?php
if ($_POST) {
// Read Matrix A
$a = [
[$_POST['a00'], $_POST['a01']],
[$_POST['a10'], $_POST['a11']]
];

// Read Matrix B
$b = [
[$_POST['b00'], $_POST['b01']],
[$_POST['b10'], $_POST['b11']]
];

// Display Matrix A
echo "<h3>Matrix A:</h3>";
echo $a[0][0] . " " . $a[0][1] . "<br>";
echo $a[1][0] . " " . $a[1][1] . "<br><br>";
// Display Matrix B
echo "<h3>Matrix B:</h3>";
echo $b[0][0] . " " . $b[0][1] . "<br>";
echo $b[1][0] . " " . $b[1][1] . "<br><br>";

// Compute the Sum


$sum = [
[$a[0][0] + $b[0][0], $a[0][1] + $b[0][1]],
[$a[1][0] + $b[1][0], $a[1][1] + $b[1][1]]
];

// Display the result


echo "<h3>Resultant Matrix (A + B):</h3>";
echo $sum[0][0] . " " . $sum[0][1] . "<br>";
echo $sum[1][0] . " " . $sum[1][1] . "<br>";
}
?>

</body>
</html>

You might also like