Lab Programs - 11-15
Lab Programs - 11-15
<?php
// Check if form data is available in the URL
if (isset($_GET['name']) && isset($_GET['age'])) {
$name = $_GET['name'];
$age = (int)$_GET['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>
<?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'];
</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>
<?php
// Function to check if number is prime
function IsPrime($n) {
if ($n <= 1) return false; // Numbers <= 1 are NOT prime
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>
<?php
// Check if form is submitted
if ($_POST) {
// Get the string from POST
$string = $_POST['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>
<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>
<?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>";
</body>
</html>