Date: Mon, 02 Jul 2001 14:02:11 -0500 (CDT) From: Mark Jacobson To: 810-088-05@uni.edu Subject: Hidden Fields, pages 314-316 of 24 Hours book Hi Perl/CGI students, ---------------------------------------------------------------------------- Also see Elizabeth Castro book: <-------- Tue, 10 Jul 2001 addition Chapter 16: Remembering What Visitors Tell You.............205 About Hidden Fields............................206 Adding Hidden Fields to a Form.................207 Storing Collected Data in a Hidden Field.......208 ---------------------------------------------------------------------------- On pages 314-316 of the Perl in 24 Hours book, Hidden Fields are covered. ------------- "The easiest way to have Web forms "remember" information is to embed previous information in them by using hidden fields. Hidden fields are part of the HTML forms specification which allows fields and values to be part of an HTML form but not appear in the form when it's rendered visually. In HTML, they're written as follows: With the preceeding HTML in any form, when that form is submitted to a PERL CGI program, the param function would return a key and a value for the HIDDEN field. param('musicianfullname') would return the value "Carlos Santana" See today's handout: The following HIDDEN FIELD is toward the very end of the FORM. ------ -------- --------------- On today's (Monday, July 2nd) handout, there is a copy of the form088Q1.cgi Perl script, that displays your 1st online quiz. As you will recall, you need to login with your user id and password to see and take this quiz. When you login successfully, I look up your 1st name and last name and store your user id in a HIDDEN FIELD named ------------ identity. Its value might be: -------- ----- SMITHJ2468:John:Smith for example, for student John Smith with user id of SMITHJ2468 Note the colons (:) are separating each of the 3 elements. $studentTriplet = join(":", $a[0], $a[1], $a[2]); Here is the Perl code that created the data for the HIDDEN FIELD named "identity" with value "SMITHJ2468:John:Smith". -------- ----- --------------------- if ($a[0] eq (uc param('userid'))) { $pw = uc param('password'); if ($a[3] eq (crypt $pw, "AC")) # $a[3] is crypted password { # of SMITHJ2468 $firstName = $a[1]; # John $lastName = $a[2]; # Smith $userID = $a[0]; # SMITHJ2468 $studentTriplet = join(":", $a[0], $a[1], $a[2]); $success = 1; last; } We will talk more about this and do some Perl exercise on Tuesday that use hidden fields. For my 9 quotes, the probability that there would be a repeated quote for any given user of my random quotes page is as follows (using the bc calculator on chaos): (9*8)/9^2 .88888888888888888888 11% chance of getting a repeated quote if you REFRESH or RELOAD random.cgi ONCE. (9*8*7)/9^3 31% chance of getting a repeated quote if .69135802469135802469 you REFRESH or RELOAD random.cgi TWICE. (9*8*7*6)/9^4 54% chance of getting a repeated quote if .46090534979423868312 you run random.cgi 4 times. In other words, if you RELOAD only 3 times, there is a better than even chance you will see only 3 quotes of the 9 quotes files!!!! (9*8*7*6*5)/9^5 74% if you view random.cgi quotes only 5 times, .25605852766346593507 there is only a 25.6% chance you will see 5 of the 9 quotes! We are going to use a hidden field in the FORM to remember what quotes have been viewed so far. -------- We might generate the random numbers: 2, then 7, then 8, then 7 <--- for example The hidden field would have the following: 8:7:2 We would use the Perl split function on the hidden field data, splitting on the : symbol that separates the number. Then we would look through the @a array of numbers and discover the number 7 had ALREADY BEEN USED (RECENTLY), so we would decide to: 1. NOT SHOW file $quotes[7] as it had already been recently viewed.... 2. and we would generate a 2nd random number, and if it wasn't in the list of numbers 8, 7 and 2, we would show that file..... For example, the next number might be 9, so quotes[9] would be opened and sent to the user's browser window. See you in class. On Tuesday and on Thursday, we are scheduled for hands-on lab classes in Wright 112 lab. Mark P.S. Suppose the user of random.cgi random quote displaying program at http://www.cns.uni.edu/~jacobson/random.cgi runs the random.cgi program 6 times in a row. What are the chances he or she will see fewer than 6 different quotes? (9*8*7*6*5*4) / 9^6 .11380379007265152669 In the bc calculator, 2 ^ 10 means 2 raised to the 10th power, so ^ is to bc and ** is to Perl. ^ is the exponentiation operator for bc, just as ** is that operator for Perl.