Bài giảng Web technologies and e-Services - Bài 11: Maintaining state through multiple forms - Trường Đại học Bách khoa Hà Nội

Maintaining state through  
multiple forms  
HTTP – stateless protocol  
HTTP is a stateless protocol  
àOnce a web server  
completes a client's request  
for a web page, the  
connection between the two  
goes away.  
àThere is no way for a  
server to recognize that a  
sequence of requests all  
originate from the same  
client.  
2
What Are Multiple-Form Web Sessions?  
A multiple-form Web session leads the user through  
a series of HTML forms that work together and pass  
data from form to form.  
E.g.  
To build a shopping cart or on-line survey.  
To save user authentication information from page to  
page  
To store persistent user preferences on a site  
3
Example Multiple Screen Session  
4
How to maintain the state through  
multiform?  
Use tricks to keep track of state information  
between requests (session tracking)  
Using hidden form fields  
URL rewriting: every local URL on which the user might  
click is dynamically modified to include extra  
information  
Using cookies: a bit of information that the server give to  
a client à depends on the client  
Using session  
5
Content  
2. User browser cookies  
3. PHP session  
6
1. Hidden fields  
Hidden fields are part of HTML forms  
Not displayed but value can be accessed in receiving  
script like any other variable.  
7
A Full Script Example  
Consider an example script sets a hidden field  
Implements the Order Info form  
on submit sends data to order2.php  
8
PHP Script – order.html  
1. <html><head><title>Order Product</title></head><body>  
2. <form action="order2.php“ method="post">  
3. <h1> Hardware Product Order Form</h1>  
4. <br><p class="highlight">  
5. We have hammers, handsaws, and wrenches on special  
today!  
6. </p>  
7. <input type="hidden" name="sample_hidden"  
value="Welcome!">  
8. <br>Enter Item: <input text type="text" size="15”  
maxlength="20" name="product">  
9. Enter Quantity: <input text type="text" size="15”  
maxlength="20" name="quantity"><br>  
10. <br><input type="submit" value="Click To Submit">  
11. <input type = "reset" value="Reset">  
12. </form></body></html>  
9
The Output ...  
The previous code can be executed at  
10  
Receiving Hidden Fields in  
Web Sessions  
Your scripts can receive data from hidden fields like  
any other data.  
Suppose the following is stored at: order2.php  
1. <html><head><title> Order Product 2 </title> </head>  
2. <body>  
3. <form action=“order3.php“ method="post">  
4. <?php $sample_hidden = $_POST[“sample_hidden”];  
5. $product = $_POST[“product”]; $quantity =  
$_POST[“quantity”];  
6. print "<p class=“highlight”>";  
7. print "Hidden value=$sample_hidden </p><br>";  
8. print "You selected product=$product and  
quantity=$quantity";  
11  
Receiving PHP Script  
9. print "<br><br><input type=\"hidden\" name=\"product\“  
value=\"$product\"> ";  
10. print "<input type=\"hidden\" name=\"quantity\“  
value=\"$quantity\">";  
11. print "<input type=\"hidden\"  
name=\"sample_hidden\"value=\"$sample_hidden\">";  
12. print 'Please enter your name:';  
13. print '<input type="text" size="15" maxlength="20“  
name="name">';  
14. print ' and billing code: (5 digits)';  
15. print '<input type="text" size="5" maxlength="5"  
name="code">';  
16. print '<br> <input type=submit value="Process Order">';  
17. print '<input type=reset>';  
18. ?></form></body></html>  
12  
Sending email from PHP scripts  
Sometimes it is useful to send email from a PHP script:  
PHP uses mail() that by default sends e-mail via the Simple  
Mail Transfer Protocol (SMTP).  
mail(to_address, subject, message, extra_headers);  
Specify the  
Text of the email  
Specify the  
destination email  
address.  
Specify the subject line  
of the e-mail.  
Specify additional  
email headers.  
13  
Consider the following  
example …  
1. $dest='orders@hardwareville.com';  
2. $subject = 'New Hardware Order';  
3. $message = 'Enclosed is a new order for 12  
hammers.\n Thanks.';  
4. $extra = 'From: harry@hardwareville.com';  
5. mail( $dest, $subject, $message, $extra );  
14  
Consider the following full  
example …  
Implements save and notify  
Called from order2.php and saved at order3.php  
Can access variables $product, $quantity, and  
$sample_hidden sent as hidden fields from the  
Billing Info form.  
15  
The following PHP Script …  
1. <html><head><title>Order Product 3</title> </head><body>  
2. <?php  
3. $sample_hidden = $_POST[“sample_hidden”];  
quantity=$_POST[“$quantity”];  
4. $product = $_POST[“product”]; $name=$_POST[“name”];  
5. $email='orders@hardwareville.com';  
6. $body = "New Order: Product=$product Number=$quantity Cust=$name  
Code=$code";  
7. print '<font size=4>';  
8. print "<br>Sending e-mail to order handling department at $email  
... </font>";  
9. print "<br>The e-mail body is <i>: $body. </i>";  
10. $from = 'harry@hardwareville.com';  
11. $subject = "New order from $name";  
12. mail($email, $subject, $body, "From: $from");  
13. print '<br><font color="blue"> E-mail sent. Thanks for  
ordering. </font>';  
14. print "<br>By the way, sample hidden=$sample_hidden";  
15. ?></body></html>  
16  
Would have the following  
output …  
17  
Would have the following  
output …  
18  
Content  
1. Hidden fields  
3. PHP session  
19  
Using Browser Cookies …  
Cookies are small pieces of data that a Web  
application can save when a user visits the Web  
page.  
Stored on the visitor’s hard drive  
a Web page script can read the previously stored browser  
cookie data  
20  
Tải về để xem bản đầy đủ
pdf 40 trang Thùy Anh 27/04/2022 9080
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Web technologies and e-Services - Bài 11: Maintaining state through multiple forms - Trường Đại học Bách khoa Hà Nội", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_web_technologies_and_e_services_bai_11_maintaining.pdf