1 00:00:00,000 --> 00:00:00,670 2 00:00:00,670 --> 00:00:03,240 >> DAVID MALAN: Let's create a website via which freshmen can register for 3 00:00:03,240 --> 00:00:04,460 intramural sports. 4 00:00:04,460 --> 00:00:08,090 In particular, let's create a form on a web page that asks for their name, 5 00:00:08,090 --> 00:00:11,490 for their gender, for their dorm, as well as whether or not they'd like to 6 00:00:11,490 --> 00:00:12,740 be a team captain. 7 00:00:12,740 --> 00:00:14,050 Let's take a look. 8 00:00:14,050 --> 00:00:17,220 >> In advance, I've already gone ahead and created this form here. 9 00:00:17,220 --> 00:00:20,210 It's not the prettiest thing in the world, to be sure, but it does ask for 10 00:00:20,210 --> 00:00:21,560 all of those details. 11 00:00:21,560 --> 00:00:24,530 Let's go ahead and now take a look at the underlying source. 12 00:00:24,530 --> 00:00:29,950 In froshim-0.php, notice that I actually have entirely HTML. 13 00:00:29,950 --> 00:00:34,440 It turns out that a PHP file doesn't strictly need to contain PHP, but if 14 00:00:34,440 --> 00:00:38,320 it does, it will be executed by the interpreter so long as it's in between 15 00:00:38,320 --> 00:00:41,330 the open PHP tag and close PHP tag. 16 00:00:41,330 --> 00:00:44,950 >> Notice then that we have really the essence an HTML form here. 17 00:00:44,950 --> 00:00:47,170 We have the form tag, as expected. 18 00:00:47,170 --> 00:00:51,390 We have an input here, whose name is name and whose type is text, for the 19 00:00:51,390 --> 00:00:52,750 undergraduates name. 20 00:00:52,750 --> 00:00:56,860 >> We have here another input that's of type check-box in order to allow him 21 00:00:56,860 --> 00:01:00,060 or her to specify whether or not they'd like to be a team captain. 22 00:01:00,060 --> 00:01:04,250 Then we have two inputs, both whose name is gender, both of whose type is 23 00:01:04,250 --> 00:01:09,410 radio but which have different values, F for female and M for male. 24 00:01:09,410 --> 00:01:12,940 It turns out that radio buttons can be designed in this way to be mutually 25 00:01:12,940 --> 00:01:16,560 exclusive so that if we check male, that unchecks female, and if we check 26 00:01:16,560 --> 00:01:18,840 female, that unchecks male. 27 00:01:18,840 --> 00:01:22,980 >> Meanwhile, if we proceed to look below dorm, we see that there's a select 28 00:01:22,980 --> 00:01:26,220 menu, inside of which is quite a few options. 29 00:01:26,220 --> 00:01:28,240 The first of those options has no value. 30 00:01:28,240 --> 00:01:32,110 It's simply meant to ensure that the menu is indeed blank by default for 31 00:01:32,110 --> 00:01:33,150 the student's dorm. 32 00:01:33,150 --> 00:01:36,750 Below that, we have options for Apley Court, for Canaday, and every other 33 00:01:36,750 --> 00:01:38,030 freshman dormitory. 34 00:01:38,030 --> 00:01:42,160 Now down below this select menu, notice we have a final input, this one 35 00:01:42,160 --> 00:01:45,600 of type submit, whose value or label is register. 36 00:01:45,600 --> 00:01:48,080 >> So now let's take a look at the file to which this form 37 00:01:48,080 --> 00:01:49,380 is going to be submitted. 38 00:01:49,380 --> 00:01:50,930 How to know where it's being submitted-- 39 00:01:50,930 --> 00:01:54,840 well if we scroll back up higher in the file, notice that the form tag had 40 00:01:54,840 --> 00:01:59,820 an action attribute of register-0.php, and the form will be submitted to that 41 00:01:59,820 --> 00:02:02,950 page via the HTTP method post. 42 00:02:02,950 --> 00:02:06,610 Let's now take a look then a register-0.php. 43 00:02:06,610 --> 00:02:10,210 >> In register.php, we again have mostly HTML. 44 00:02:10,210 --> 00:02:12,320 But inside of the body, notice this. 45 00:02:12,320 --> 00:02:16,240 There's a pre tag which denotes preformatted text, text that should be 46 00:02:16,240 --> 00:02:18,080 rendered in a monospaced font. 47 00:02:18,080 --> 00:02:23,480 And then inside that pre tag, notice that we have here a PHP tag, open and 48 00:02:23,480 --> 00:02:28,010 close, and a call to a function called print_r, which prints recursively 49 00:02:28,010 --> 00:02:29,930 whatever you pass in as its argument. 50 00:02:29,930 --> 00:02:33,690 >> In this case, I'm passing in recursively a superglobal variable 51 00:02:33,690 --> 00:02:36,080 called $_post. 52 00:02:36,080 --> 00:02:40,530 It's in this superglobal, this associative array, that any HTTP 53 00:02:40,530 --> 00:02:44,980 parameters or form fields that were submitted by the user will end up for 54 00:02:44,980 --> 00:02:46,040 us to access. 55 00:02:46,040 --> 00:02:50,160 Let's now take a look in a browser at froshim-0.php. 56 00:02:50,160 --> 00:02:53,710 >> Let's go ahead and register myself as David Malan. 57 00:02:53,710 --> 00:02:57,580 I'll be a captain, and I'll be a male in, say, Matthews. 58 00:02:57,580 --> 00:02:59,120 Let's now click Register. 59 00:02:59,120 --> 00:03:01,210 >> And now, notice we see hideous result. 60 00:03:01,210 --> 00:03:04,420 But this is simply the result of printing in preformatted text-- that 61 00:03:04,420 --> 00:03:05,770 is, a monospaced font-- 62 00:03:05,770 --> 00:03:08,070 the contents of that associative array. 63 00:03:08,070 --> 00:03:11,100 In particular, notice that what the browser ultimately submitted to the 64 00:03:11,100 --> 00:03:16,380 server is a name whose value is David Malan; a field called captain, whose 65 00:03:16,380 --> 00:03:20,020 value is apparently on, which seems to be the default value when I check a 66 00:03:20,020 --> 00:03:24,670 check-box; gender, whose value is M for male; and dorm, 67 00:03:24,670 --> 00:03:26,430 whose value is Matthews. 68 00:03:26,430 --> 00:03:28,490 Now I haven't yet done anything with these values. 69 00:03:28,490 --> 00:03:33,230 But clearly, because PHP is bundling them up for me in this superglobal 70 00:03:33,230 --> 00:03:37,760 called $_post, I'll surely have programmatic access to do something 71 00:03:37,760 --> 00:03:40,010 with those variables, like register this freshman. 72 00:03:40,010 --> 00:03:41,260