Lab Tasks
Lab Tasks
Lab Tasks
Task 1 Create a simple HTML form and accept the user’s name and display the name through PHP
echo statement
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method='POST' action="#">
<h2>Please Enter your name:</h2>
<input type="text" name="name">
<input type="submit" name="register" value="Submit Name">
</form>
<?php
if(isset($_POST["register"])) {
$name = $_POST['name'];
echo "<h3> Hello $name </h3>";
}
?>
</body>
</html>
Task 2 Write a PHP program to find the length of the string entered by user.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method='GET' action="#">
<h2>Please Enter the string:</h2>
<input type="text" name="str">
<input type="submit" name="check" value="Check length">
</form>
<?php
if(isset($_GET["check"])) {
$mystring = $_GET['str']; //
echo "$mystring length is ".strlen($str);
}
?>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method='POST' action="#">
<h2>Please enter the number:</h2>
<input type="number" name="num" min='1' max='7' required>
<input type="submit" name="check" value="OK">
</form>
<?php
if(isset($_POST["check"])) {
$day = $_POST['num']; //
switch ($day) {
case "1":
echo "It is Monday!";
break;
case "2":
echo "It is today!";
break;
case "3":
echo "It is Wednesday!";
break;
case "4":
echo "It is Thursday!";
break;
case "5":
echo "It is Friday!";
break;
case "6":
echo "It is Saturday!";
break;
case "7":
echo "It is Sunday!";
break;
default:
echo "Invalid number!";
}
}
?>
</body>
</html>
data.php
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['check_list'])) {
// Counting number of checked checkboxes.
$numofchkd = count($_POST['check_list']);
$name = $_POST['username'];
echo $name . " 's favourite colours are ". $numofchkd."
option(s): <br/>";
// Loop to store and display values of each checked checkbox.
foreach ($_POST['check_list'] as $selected) {
echo "<p>" . $selected. "</p>";
}
}
else {
echo "<b>Please Select At least One Option. </b>";
}
}
?>
<html>
<head> <title> array to string </title> </head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
method="post">
<input type="text" name="user" placeholder="username" required/><br/>
<input type="password" name="pass" placeholder="password"
required/><br/>
<input type="submit" name="login" value="Login"/><br/>
</form>
<?php
if(isset($_POST["login"])) {
if (! empty($_POST['user'] && $_POST['pass'])) {
$username=$_POST['user'];
$password=$_POST['pass'];
if ($username=="admin" && $password=="1234") {
header ("Location: home.php");
}
else {
echo "Incorrect username or password, please try
again!";
}
}
else {
echo "Please fill out the required fields";
}
}
?>
</body>
</html>
<!DOCTYPE html>
<head>
<title>Reading a file using PHP</title>
</head>
<body>
<?php
$filename = "mytext.txt";
$file = fopen($filename, "r" );
if( $file == false )
{
echo ( "Error in opening file" );
exit();
}
$filesize = filesize( $filename );
$filetext = fread( $file, $filesize );
fclose( $file );
echo ( "File size : $filesize bytes" );
echo ( "<pre>$filetext</pre>" );
?>
</body></html>
Task 2 : Creating a new text file and then writing a short text heading in site it.
<!DOCTYPE html>
<head>
<title>Writing to the file using PHP</title>
</head>
<body>
<?php
$filename = "mytext.txt";
$file = fopen($filename, "w" );
if( $file == false ){
echo ( "Error in opening new file" );
exit();
}
fwrite( $file, "This is a simple test\n" );
fclose( $file );
</body>
</html>
</body>
</html>
<?php
if( $_FILES['file']['name'] != "" ) {
copy( $_FILES['file']['name'], "myupload/" ) or
die( "Could not copy file!") }
else {
die("No file specified!"); }
?>
<?php
function addFive($num){
$num += 5;
}
function addSix(&$num){
$num += 6;
}
$orignum = 10;
addFive( $orignum ); //pass by value
echo "Original Value is $orignum<br />";
addSix( $orignum ); //pass by reference
echo "Original Value is $orignum<br />";
?>
Assume that database is created with name: my_personal_contacts having a table named:
my_contacts (ID, Full Names, Gender, Contact No, Email, City, Country)
<?php
if (!mysqli_select_db($dbh,'my_personal_contacts'))
?>
<?php
Include “dbconnection.php”;
$sql_stmt = "INSERT INTO my_contacts VALUES('Peter','Male','541',' peter @
gmail.com ','AA','Ethiopia')";
$result = mysqli_query($dbh,$sql_stmt); //execute SQL statement
if (!$result)
die("Adding record failed: " . mysqli_error());
else
echo "Peter has been successfully added to your contacts list";
mysqli_close($dbh);
?>
<?php
include “dbconnection.php”;
$sql_stmt = "SELECT * FROM my_contacts";
$result = mysqli_query($dbh,$sql_stmt);
if (!$result)
die("Database access failed: " . mysqli_error());
$rows = mysqli_num_rows($result);
if ($rows) {
while ($row = mysqli_fetch_array($result)) {
echo 'ID: ' . $row['id'] . '<br>';
echo 'Full Names: ' . $row['full_names'] . '<br>';
echo 'Gender: ' . $row['gender'] . '<br>';
echo 'Contact No: ' . $row['contact_no'] . '<br>';
echo 'Email: ' . $row['email'] . '<br>';
echo 'City: ' . $row['city'] . '<br>';
echo 'Country: ' . $row['country'] . '<br><br>';
}
}
mysqli_close($dbh); //close the database connection
?>