Vous êtes sur la page 1sur 8

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP

Lab 2- HTML Forms and PHP Name: _______________________ Metric No: ____________________ Section: ______________________ Instruction: Please adopt the program given to the PHP programming. Prepare the output of every html and php codes.

1)

Name the file with hello.html. <html> <head> <title>Greetings!</title> </head> <body> <div><p>Click a link to say hello:</p> <ul> <li><a href=hello.php?name=Michael>Michael</a></li> <li><a href=hello.php?name=Celia>Celia</a></li> <li><a href=hello.php?name=Jude>Jude</a></li> <li><a href=hello.php?name=Sophie>Sophie</a></li> </ul> </div> </body> </html>

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP

2)

Name the file with hello.php <html> <head> <title>Greetings!</title> </head> <body> <?php init_set(display_errors, 1); error_reporting(E_ALL | E_STRICT); $name= $_GET[name]; print <p>Hello, <span style=\font-weight: bold;\>$name</span>!</p>; ?> </body> </html>

3)

Name the file with calculator.html <html> <head> <title>Product Cost Calculator</title> </head> <body> <div><p>Fill out this form to calculate the total cost:</p> <form action=handle_calc.php method=post> <p>Price: <input type=text name=price size=5 /></p> <p>Quantity: <input type=text name=discount size=5 /></p> <p>Tax: <input type=text name=tax size=3 /> (%)</p> 2

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP

<p>Shipping method: <select name=shipping> <option value=5.00>Slow and steady</option> <option value=8.95>Put a move on it.</option> <option value=19.36>I need it yesterday!</option> </select></p> <p>Number of payments to make: <input type=text name=payments size=3 /></p> <input type=submit name=submit value=Calculate! /> </form> </div> </body> </html>

4)

Name the file with handle_calc.php <html> <title>Product Cost Calculator</title> <style type=text/css media=screen> .number { font-weight: bold; } </style> </head> <body> 3

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP <?php /*This script takes values from calculator.html and performs total cost and monthly payment calculations. */ $price = $_POST[price]; $quantity = $_POST[quantity]; $discount = $_POST[discount]; $tax = $_POST[tax]; $shipping = $_POST[shipping]; $payments = $_POST[payments]; //Calculate the total: $total = $price * $quantity; $total = $total + $shipping; $total = $total - $discount; //Determine the tax rate: $taxrate = $tax/100; $taxrate = $taxrate + 1; //Factor in the tax rate; $total = $total * taxrate; //Calculate the monthly payments: $monthly = $total/ $payments; //Print out the results: print <div>You have selected to purchase:<br /> <span class=\number\>$quantity</span>widget(s) at<br /> $<span class=\number\>$price</span>price each plus a<br /> $<span class=\number\>$shipping</span>shipping cost and a<br /> <span class=\number\>$tax</span>percent tax rate.<br /> After your $<span class=\number\>$discount</span>discount, the total cost is $<span class=\number\>$total</span>.<br /> Divided over <span class=\number\>$payments</span>monthly payments, that would be $<span class=\number\>$monthly</span>each.</p></div>; ?> </body> </html> 5) Name the file with random.php <html> <head>

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP <title>Lucky Numbers</title> </head> <body> <?php /*This script generates 3 random numbers. */ //Create three random numbers.*/ $n1 = rand(1, 99); $n2 = rand(1, 99); $n3 = rand(1, 99); //Print out the numbers: print <p>Your lucky numbers are:<br /> $n1<br /> $n2<br /> $n3<br /> ?> </body> </html> Exercises 1) 2) 3) 4) 5) 6) HTTP is a stateless protocol. Describe in details about that statements. Discuss the effect of the stateless protocol. Discuss the way that we can use to create statefullness in Web applications. Define the solution of Question 3 with the detail elaborations. Describe the advantage(s) from the conclusion in Question 4. The login form is one of the requirements for developing the websites. Here, the form is using sessions to keep track the user login. Describe the advantage of using session and give the example of using them. 7) Try to figure out this login form when you are free. Discuss with the lecturer if you have encounter the problems. This may use related database to authenticate the user who wants to login the form.

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP 6) Name the file with login.php function show_form($message) { print "<div><h1>$message</h1><h2>Login</h2> <form action=\"$_SERVER[PHP_SELF]\" method=\"post\"> <div><input type=\"hidden\" name=\"submitted\" value=\"1\" /></div><p>Username <input type=\"text\" name=\"username\" maxlength=\"15\" value=\"$_POST[username]\" /></p><p>Password <input type=\"password\" name=\"pass\" value=\"$_POST[pass]\"/></p><p><input type=\"submit\" value=\"Login\" /> Not yet a member? <a href=\"create_account.php\">Create an account</a>! </p></form></div>"; } function process_form() { $username=trim($_POST['username']); $pass=trim($_POST['pass']); $sha_pass=sha1($pass); $db=mysql_connect('localhost','krichel','laempel'); $query="SELECT * FROM beer_shop.users WHERE username='$username' AND password = '$sha_pass'"; $result=mysql_query($query); $error=mysql_error(); if($error) { return "Sorry: $query gives an error<br/> $error"; } $affected=mysql_affected_rows(); if(! ($affected)) {return "Invalid username or password";} } if($_POST['submitted']) { $error=process_form(); if($error) { show_form($error); } else { $user=$_POST['username']; print "<h1>Welcome to $user</h1>"; } } else { show_form(''); } 7) Name the file with create_account.php function show_form($message) { print "<div><h1>$message</h1><h2>Create Account</h2><p>Please complete the form below to create your account. </p> <form action=\"$_SERVER[PHP_SELF] \" method=\"post\"><div><input type=\"hidden\" name=\"submitted\" value=\"1\" /></div> It must be more

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP than 5 characters and cannot be your username.</p><p> <input type=\"submit\" value=\"Create Account\" /> </p></form></div>"; <h3>Password</h3><p> Password <input type=\"password\" name=\"pass1\" value=\"$_POST[pass1]\"/>Confirm Password <input type=\"password\" name=\"pass2\" value=\"$_POST[pass2]\"/> </p><p>The password you enter will be used to access your account. It must be more than 5 characters and cannot be your username.</p> <p><input type=\"submit\" value=\"Create Account\" /></p></form></div>"; } function process_form() { $username=trim($_POST['username']); $pass1=trim($_POST['pass1']); $pass2=trim($_POST['pass2']); if(strlen($username)<6) { return "Username is too short."; } if(! ($pass1 == $pass2)) {return "Passwords do not match.";} $pass=$pass1; if($pass == $username) { return "Your username can not be your password."; if(strlen($pass)<6) {return "Password is too short.";} $sha_pass=sha1($pass); $db=mysql_connect('localhost','krichel','laempel'); $query="INSERT INTO beer_shop.users VALUES ('','$username','$sha_pass')"; $result=mysql_query($query); $error=mysql_error(); if($error == "Duplicate entry '$username' for key 2") { return "Sorry: Username $username is already taken, choose another."; } else {print "<h1>Thank you for registering with us!</h1>";} } if($_POST['submitted']) { $error=process_form(); if($error) { show_form($error); } } else { show_form(''); } 8) Name the file with visit.php

DIT 3243- INTRODUCTION TO WEB PROGRAMMING WITH PHP <?php $top='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><head><title></title><meta http-equiv="content-type" content="text/html; charset=UTF-8"/> </head><body><div>'; $bottom='</div><p> <a href="http://validator.w3.org/check?uri=referer"> <img style="border: 0pt" src="/valid-xhtml10.png" alt="Valid XHTML 1.0!" height="31" width="88" /> </a></p></body></html>'; session_start(); $current=mktime(); // look at the current time if($_SESSION[last_click]) { $passed=$current-$_SESSION[last_click]; $to_print.="$passed seconds have passed since your last visit.\n"; $_SESSION[last_click]=$current; } else { $to_print="This is your first visit.\n"; $_SESSION[last_click]=$current; } print "$top\n$to_print\n$bottom"; ?>

Vous aimerez peut-être aussi