Lab Tasks

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Lab Task (Chapter 2)

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>

By: Chala Simon May-21


Task 3 Write a program to show day of the week (for example: Monday) based on numbers given
from users.

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

By: Chala Simon May-21


Task 4 Write a program to display the name and favorite colors of the user which is given from user
form.
index.php
<html>
<head> <title> array to string </title> </head>
<body>
<form action="data.php" method="post">
Username: <input type="text" name="username" placeholder="enter name"
required/><br/><br/>
Select your favourite colors:<br/>
Red<input type="checkbox" name="check_list[]" value="red"/><br/>
Blue<input type="checkbox" name="check_list[]" value="blue"/><br/>
Green<input type="checkbox" name="check_list[]" value="green"/><br/>
Yellow<input type="checkbox" name="check_list[]" value="yellow"/><br/>
Pink<input type="checkbox" name="check_list[]" value="pink"/><br/>
Black<input type="checkbox" name="check_list[]" value="black"/><br/>
White<input type="checkbox" name="check_list[]" value="white"/><br/>
<input type="submit" name="submit" value="Submit"/><br/>
</form>
</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>";
}
}
?>

By: Chala Simon May-21


Task 5 Create a login form with two text fields called “login” and “password”. When user enters
“admin” as a user name and “1234” as a password it should be redirected to a home.php page
or displays “incorrect username or password” in case of wrong username/password
index.php

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

By: Chala Simon May-21


Lab Task (Chapter 3)
Task 1 : Reading the file using PHP Script

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

By: Chala Simon May-21


Task 3 : Creating an upload form
<!DOCTYPE html>
<head>
<title> upload form </title>
</head>
<body>
<form action="file_uploader.php" method="post”
enctype="multipart/form-data">
<input type="file" name="file" /><br />
<input type="submit" value="Upload File" />
</form>

</body>

</html>

: File Upload Php Script (uploader.php )

<?php
if( $_FILES['file']['name'] != "" ) {
copy( $_FILES['file']['name'], "myupload/" ) or
die( "Could not copy file!") }
else {
die("No file specified!"); }

echo “Sent file: ”.$_FILES['file']['name'];


echo “File size: ”.$_FILES['file']['size'];
echo “File type: ”.$_FILES['file']['type’];

?>

Task 4 Variable passing in Function

<?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 />";

?>

By: Chala Simon May-21


Lab Task (Chapter 4)
Task 1 : Database Connection

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

$dbh = mysqli_connect('localhost', 'root’, ‘pass’);

die("Unable to connect to MySQL: " . mysqli_error());

if (!mysqli_select_db($dbh,'my_personal_contacts'))

die("Unable to select database: " . mysqli_error());

?>

Task 2 : Inserting new records

<?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);
?>

By: Chala Simon May-21


Task 3 Reading records from database

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

By: Chala Simon May-21

You might also like