Dynamic Web Pages 2009v2.1

4,027
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,027
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
54
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Dynamic Web Pages 2009v2.1

  1. 1. 28/06/2009 Dynamic Web Pages PHP 1 Authors & Modifications First Edition 2006 Version 1.0 1.1 2.0 2.1 No. Authors Date 07/06 08/06 07/08 07/09 Cathie Usher Contact Cathie Usher Cathie Usher Cathie Usher Cathie Usher 2 Database php version 2.1 1
  2. 2. 28/06/2009 Chapter 1 Site management & Sending & Receiving data between pages. 3 Dynamic web page decisions • The first decision is to decide what scripting language you are going to p g g g y g g write your dynamic pages in. – PHP: Generally uses an Apache server with php. – ASP: Generally uses Microsoft Internet Information Ser ces ( S), wh ch has nformat on Services (IIS), which an asp engine incorporated. 4 Database php version 2.1 2
  3. 3. 28/06/2009 How PHP Pages are Accessed and Interpreted Your PC WebServer (Internet connected) (Internet connected) 1. Web Browser 2. Se Web Server nd Re ques Please Enter t for PHP Software file A Phone 3. Receive Number request, find file and read it. Submit Erase 4. Execute PHP statements 7. Web Browser 5. Send Web Browser results Phone Query back. back lts Results: nR esu etur That is 6. R John Doe's Phone Number We will use WAMPS (Windows, apache, mysql, php server) and it will be locally installed on the lab machines. 5 WAMPS • This web service has three servers; – Apache p – MYSQL – PHP • Wamps will be located locally on the lab machines. So the lab machines will be local and server machine. b al l ds hi 6 Database php version 2.1 3
  4. 4. 28/06/2009 Creating a PHP Script File and Saving It to a Local Disk You can use a number of different editors to create your PHP script files. P P fl – The PHP script starts with a <?php tag and ends with ?>. – Between these tags is a single PHP print statement. 7 Alternative PHP Delimiters • You can alternatively start your PHP scripts with the <script> tag as follows: p p g f <script language="PHP"> print ("A simple initial script"); </script> • If have short_open_tag enabled in its configuration file, you can use <? and ?>. • If asp_tags is enabled in the PHP configuration file, you can use <% and %> as delimiters. 8 Database php version 2.1 4
  5. 5. 28/06/2009 A Little About PHP's Syntax • Some PHP Syntax Issues: – Be careful to use quotation marks, parentheses, and brackets in pairs. – Most PHP commands end with a semicolon (;). – Be careful of case. – PHP ignores blank spaces. l 9 Embedding PHP Statements Within HTML Documents • One way to use PHP is to embed PHP scripts within HTML tags in an HTML document. 1. <html> 2. <head> 3. <title>HTML With PHP Embedded</title> </head> 4. <body> 5. <font size=5 color=”blue”>Welcome To My Page</font> 6. <?php 7. print ("<br> Using PHP is not hard<br>"); 8. 8 ?> 9. and you can learn to use it quickly! 10. </body></html> 10 Database php version 2.1 5
  6. 6. 28/06/2009 Using Backslash () to Generate HTML Tags with print() • Sometimes you want to output an HTML tag that also requires double quotation h l d bl marks. – Use the backslash (“”) character to signal that the double quotation marks themselves should be output: print ("<font color="blue">"); – The above statement would output: <font color="blue"> 11 Using Comments with PHP cripts • Comments enable you to include y descriptive text along with the PHP script. – Comment lines are ignored when the script runs; they do not slow down the run-time. – Comments have two common uses. • Describe the overall script purpose. • Describe particularly tricky script lines. 12 Database php version 2.1 6
  7. 7. 28/06/2009 Using Comments with PHP Scripts • Comment Syntax - Use // y <?php // This is a comment ?> • Can place on Same line as a statement: <?php print ("A simple initial script"); //Output a ( A script ); line ?> 13 Example Script with Comments 1. <html> <head> 2. <title> Generating HTML From PHP</title> </head> g / / 3. <body> <h1> Generating HTML From PHP</h1> 4. <?php 5. // 6. // Example script to output HTML tags 7. // 8. print ("Using PHP has <i>some advantages:</i>"); 9. print ("<ul><li>Speed</li><li>Ease of use</li> <li>Functionality</li></ul>"); //O li i li /li / l ") //Output b ll bullet li list 10. print ("</body></html>"); 11. ?> 14 Database php version 2.1 7
  8. 8. 28/06/2009 Alternative Comment Syntax PHP allows a couple of additional ways to create comments. <?php phpinfo(); # This is a built-in function ?> • Multiple line comments. <?php /* A script that gets information about the PHP version being used. */ <? phpinfo(); ?> 15 Using PHP Variables • Variables are used to store and access data in computer memory. p y • A variable name is a label used within a script to refer to the data. Name of variable $cost = 4.25; Variables new value $months = 12; 16 Database php version 2.1 8
  9. 9. 28/06/2009 Assigning New Values to Variables • You can assign new values to variables: $days = 3; $newdays = 100; $days = $newdays; • At the end of these three lines, $days and $newdays both have values of 100. 17 Selecting Variable Names • You can select just about any set of characters for a variable name in PHP PHP, but they must: – Use a dollar sign ($) as the first character – Use a letter or an underscore character (_) as the second character. • N Note:Try to select variable names that T l i bl h help describe their function. For example $counter is more descriptive than $c or $ctr. 18 Database php version 2.1 9
  10. 10. 28/06/2009 Combining Variables and the print Statement • That is, to print out the value of $x is $x, write the following PHP statement: – print ("$x"); • The following code will output “Bryant is 6 years old”. $age=6; print ("Bryant is $age years old."); 19 A Full Example ... 1. <html> 2. 2 <head> <title>Variable Example </title> </head> 3. <body> 4. <?php 5. $first_num = 12; 6. $second_num = 356; 7. $temp = $first_num; 8. $first_num = $second_num; 9. $second_num = $temp; 10. print ("first_num= $first_num <br> second_num=$second_num"); 11. ?> </body> </html> 20 Database php version 2.1 10
  11. 11. 28/06/2009 Using Arithmetic Operators • You can use operators such as a plus sign (+) for addition and a minus sign (–) for ( ) subtraction to build mathematical expressions. • For example <?php $apples = 12; $oranges = 14; $total_fruit = $apples + $oranges; print ("The total number of fruit is $total_fruit"); ?> • These PHP statements would output “The total number of fruit is 26.” 21 Common PHP Numeric Operators Table 2.1 Common PHP Numeric Operators Operator Effect Example Result + Addition $x = 2 + 2; $x is assigned 4. - Subtraction $y = 3; $y is assigned 2. $y = $y – 1; / Division $y = 14 / 2; $y is assigned 7. * Multiplication $z = 4; $y is assigned 16. $y = $z * 4; % Remainder $y = 14 % 3; $y is assigned 2. 22 Database php version 2.1 11
  12. 12. 28/06/2009 A Full Example 1. <html> 2. <head> <title>Variable Example </title> </head> 3. <body> y 4. <?php 5. $columns = 20; 6. $rows = 12; 7. $total_seats = $rows * $columns; 8. 9. $ticket_cost = 3.75; 10. $total_revenue = $total_seats * $ticket_cost; 11. 12. $building_cost = 300; 13. $profit = $total revenue - $building cost; 14. 15. print ("Total Seats are $total_seats <br>"); 16. print ("Total Revenue is $total_revenue <br>"); 17. print ("Total Profit is $profit"); 18. ?> </body> </html> 23 WARNING: Using Variables with Undefined Values If you accidentally use a variable that does not have a value assigned to it will have no value (called a null value).When a variable with a null value is used in an expression PHP, PHP may not generate an error and may complete the expression evaluation. For example, the following PHP script will output x= y=4. <?php $y = 3; $y=$y + $x + 1; // $x has a null value print ("x=$x y=$y"); ?> 24 Database php version 2.1 12
  13. 13. 28/06/2009 Writing Complex Expressions • Operator precedence rules define the d in hi h th th order i which the operators are t evaluated. For example, $x = 5 + 2 * 6; • The value of $x is either 42 or 17 depending on order of evaluation. p g f • Since multiplication evaluated before addition operations, this expression evaluates to 17. 25 PHP Precedence Rules • PHP follows the precedence rules listed below. – First it evaluates operators within parentheses. – Next it evaluates multiplication and division di i i operators. t – Finally it evaluates addition and subtraction operators. 26 Database php version 2.1 13
  14. 14. 28/06/2009 PHP Precedence Rules • For example, the first 2 statements evaluate to 80 while the last to 180. – $x = 100 - 10 * 2; – $y = 100 - (10 * 2); – $z = (100 - 10) * 2; 27 A Full Example 1. <html> 2. 2 <head> <title>Expression Example </title> </head> 3. <body> 4. <?php 5. $grade1 = 50; 6. $grade2 = 100; 7. $grade3 = 75; 8. $average = ($grade1 + $grade2 + $grade3) / 3; 9. print ("The average is $average"); 10. ?> </body> </html> 28 Database php version 2.1 14
  15. 15. 28/06/2009 Working with PHP String Variables • Character strings are used in scripts to hold data such as customer names names, addresses, product names, and descriptions. • Consider the following example. – $name="Christopher"; – $preference="Milk Shake"; • $name is assigned “Christopher” and the variable $preference is assigned “Milk Shake”. 29 WARNING: Be Careful Not to Mix Variable Types • Be careful not to mix string and numeric variable types. • For example, you might expect the following statements to generate an error message, but they will not. Instead, they will output “y=1”. <?php $x ="banana"; $sum = 1 + $x; print ("y=$sum"); ?> 30 Database php version 2.1 15
  16. 16. 28/06/2009 Using the Concatenate Operator • The concatenate operator combines two separate string variables i t one. t t i i bl into • For example, – $fullname = $firstname . $lastname; • $fullname will receive the string values of $firstname and $lastname connected together. g • For example, $firstname = "John"; $lastname = "Smith"; $fullname = $firstname . $lastname; print ("Fullname=$fullname"); 31 TIP; An Easier Way to Concatenate Strings • You can also use double quotation marks to q create • concatenation directly, • For example, • $Fullname2 = "$FirstName $LastName"; • Thi statement h This t t t has th same effect as the ff t • $Fullname2 = $FirstName . " " . $LastName; 32 Database php version 2.1 16
  17. 17. 28/06/2009 The strlen() Function • Most string functions require you to send them one or more arguments. th t • Arguments are input values that functions use in the processing they do. • Often functions return a value to the script based on the input arguments For arguments. example $len = strlen($name); Receives the number of Variable or value to work with characters in $name Name of function 33 The strlen() Function Example <?php p p $comments = "Good Job"; $len = strlen($comments); print ("Length=$len"); ?> This PHP script would output “Length=8”. p p g 34 Database php version 2.1 17
  18. 18. 28/06/2009 The trim() Function • This function removes any blank characters y from the beginning and end of a string. For example, consider the following script: – <?php – $in_name = " Joe Jackson "; – $name = trim($in_name); – print ("name=$name$name"); – ?> 35 The strtolower() and strtoupper() Functions • These functions return the input string in all uppercase or all l pp s ll lowercase l tt s respectively. s letters, sp ti l • For example, <?php $inquote = "Now Is The Time"; $lower = strtolower($inquote); $upper = strtoupper($inquote); print ("upper=$upper lower=$lower"); ?> • The above would output “upper=NOW IS THE TIME lower=now is the time”. 36 Database php version 2.1 18
  19. 19. 28/06/2009 The substr() Function – Substr has the following general format: Assign the Starting position to extracted sub- start extraction from. string into this $part = substr( $name, 0, 5); variable. Number of characters to extract. (If omitted it will Extract from this continue to extract until the end string variable variable. of the string.) f th t i ) 37 The substr() Function • The substr() function enumerates character positions starting with 0 (not 1) 1), – For example, in the string “Homer”, the “H” would be position 0, the “o” would be position 1, the “m” position 2, and so on. • For example, the following would output “Month=12 Day=25”. <?php $date = "12/25/2002"; $month = substr($date, 0, 2); $day = substr($date, 3, 2); print ("Month=$month Day=$day"); ?> 38 Database php version 2.1 19
  20. 20. 28/06/2009 The substr() Function • As another example, consider the following use of the substr() function – It does not include the third argument (and thus returns a substring from the starting position to the end of the search string). <?php $date = "12/25/2002"; $year = substr($date, 6); print ("Year=$year"); ?> • The above script segment would output “Year=2002”. 39 Servers and files • The php files need to be located on the server. • The location for wamps is C:wampswww • All files must be place here. • In dreamweaver you will work on files that are local on your storage space: f, h etc. • When these local files are save they will automatically be uploaded to the server. 40 Database php version 2.1 20
  21. 21. 28/06/2009 Dynamic Web pages requirements • Create a local site: shown on the next slide. • Check WAMPS is running. – On the lab machines sometimes the IIS starts first and blocks the port. If this happens you must stop the IIS. • Create a dynamic and testing site: shown on the following slides. 41 Creating a local site Local folder is your y H or F drive etc. 42 Database php version 2.1 21
  22. 22. 28/06/2009 Creating a remote site Note: You will have to create this folders, newlandStart. This can be done using the browse button. wampwww is the default directory. 43 Creating a Testing Server 44 Database php version 2.1 22
  23. 23. 28/06/2009 Transferring to the Remote server • Click on the “expand show local & remote sites” button. • Click on the “put files” button; the blue arrow. 45 Changing htm to php • Change all the htm & html pages to php. • This is done by click on the name in the file panel and changing the extension. (same as renaming in windows). • An update screen will appear. appear • Click ok. 46 Database php version 2.1 23
  24. 24. 28/06/2009 Check the pages • Open each page one at a time followed by the “f12” key to execute y y the page in the browser. • Once all pages have been executed you should be able to test the links on each page. • If th li k are not working, check the links t ki h k they are to the asp extension pages. 47 Using forms to send data. • Overview: – In this section you will create pages with embedded forms. ith b dd d f – The forms will have objects, such as text fields, submit buttons, radio buttons etc. – The data in the form objects will be sent to the server and be displayed on another page. 48 Database php version 2.1 24
  25. 25. 28/06/2009 Retrieving data • Overview: – The are four ways of transferring data y g from one page to another. • Through a form with the post method. • Through a form with the get method. • Through a URL link • Through a cookie. 49 Creating HTML Input Forms • HTML Forms are not part of PHP language but important way to send data to scripts Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button 50 Database php version 2.1 25
  26. 26. 28/06/2009 Starting And Ending HTML Forms • You can create HTML forms by using the HTML <form> and </form> tags tags. Program to start when form is submitted. <form action="http://webwizard.aw.com/~phppgm/program.php" method="post"> Format to send data. . . Place form elements between <form> and </form> tags. </form> . Forms end with </form> 51 Creating Form Buttons • You can create submit and reset buttons by placing the following within <form> & </form> tags. <input type=”submit” value=”Click To Submit”> <input type=”reset” value=”Erase and Restart”> Type of button to create Button Label • The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase and Restart”. 52 Database php version 2.1 26
  27. 27. 28/06/2009 Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 3 <body> 4.<form action="http://webwizard.aw.com/~phppgm/First.p hp" method="post" > 5. Click submit to start our initial PHP program. 6. <br> <input type="submit" value="Click To p yp Submit"> 7. <input type="reset" value="Erase and Restart"> 8. </form> 9. </body> </html> 53 Receiving Form Input into PHP Scripts • To receive HTML form input into a PHP script: – Use a PHP var name that matches the variable defined in the form element’s name argument. • For example, if form uses the following: – <input type="radio" name="contact" value="Yes"> • Then form-handling PHP script could use a variable called $ $contact. – If the user clicks the radio button, then $contact would = Yes 54 Database php version 2.1 27
  28. 28. 28/06/2009 Full Example • Suppose your HTML form uses the following: – Enter email address: <input type="text" size="16" maxlength="20" name="email"> g • Then can receive input as follows: 1.<html> 2. <head><title> Receiving Input </title> </head> 3. <body> 4. <font size=5>Thank You: Got Your Input.</font> 5. 5 <?php 6. print ("<br>Your email address is $email"); 7. 8. print ("<br> Contact preference is $contact"); 9. ?> 55 Register_Globals? • Since PHP 4.2.1, the default PHP configuration is requiring a different mechanism t receive i h i to i input f security t for it reasons (than the one just shown). – Technical details: it is a PHP configuration option to turn REGISTER_GLOBALS OFF (new default) or ON in the php.ini configuration file. • If your site has REGISTER_GLOBALS OFF you must use a different mechanism to receive HTML Form Variables. 56 Database php version 2.1 28
  29. 29. 28/06/2009 How can you tell if Register_Globals is OFF? • Enter the following PHP script and run it. g p – <?PHP phpinfo(); ?> • Search through the output for REGISTER_GLOBALS and see if it is set to OFF or ON. • If it i off you must use th f ll i way t is ff t the following to receive input data. 57 Getting input data with Register_Globals OFF? • To receive data with REGISTER_GOBALS OFF you use a special variable called $POST. FF l bl ll d $P Enclose in square – $name $_POST[“name”]; bracket and then quotes Name of HTML form variable (note do not use $) i bl ( t d t Special PHP Global variable. Technically it is an associative array (covered in chptr 5.) PHP variable name that you want to receive 58 the HTML form input. Database php version 2.1 29
  30. 30. 28/06/2009 Full Example, when REGISTER_GLOBALS is OFF. • Suppose your HTML form uses the following: – Enter email address: <input type="text" size="16" maxlength="20" name="email"> • Then can receive input as follows: 1. <html> 2. <head><title> Receiving Input </title> </head> 3. <body> y 4. <font size=5>Thank You: Got Your Input.</font> 5. <?php 6. $email = $_POST[“email”]; 7. $contact = $_POST[“contact”]; 8. print ("<br>Your email address is $email"); 9. print ("<br> Contact preference is $contact"); 59 10. ?> Forms sending & retrieving 1 • Create a new page and save as test_form.php. • Title : Using a form. • Add a form, 2 textfield and a submit button. • The page should look like; 60 Database php version 2.1 30
  31. 31. 28/06/2009 Forms sending & retrieving 2 • Properties of objects • Textfield 1: – N Name= firstName fi tN • Textfield 2: – Name=lastName • Form – Name=frmName – Action=test form processor php Action=test_form_processor.php – method-=POST. 61 Forms sending & retrieving 3 • Create another page called p p p test_form_processor.php. • Title: Displaying form data. • Create the page as shown and select the plus to bind the form data. 62 Database php version 2.1 31
  32. 32. 28/06/2009 Forms sending & retrieving 4 • Select the form variable. • Select the Request.Form type. • The name should be the same as the name on the page the data is coming from. f In this case the data is coming from test_form.php 63 Forms sending & retrieving 5 • Place the cursor after the comer after the “Thank you ”. • Click on the binding firstName and then click on the insert button. This inserts the request variable into that space on the page. • To insert the binding you can also drag and drop as another method. d th th d • Repeat this for the lastName variable. 64 Database php version 2.1 32
  33. 33. 28/06/2009 Forms sending & retrieving 6 • Your page should look like this. • Now execute the test form php test_form.php page. Note: You will need to put a non breaking space (&nbsp;) between the two form bindings 65 Looking at the code. • Normally php code is embedded with <?php ?> • Echo is the command to display information. There are two other commands: print() and printf(). • The two print command allow formatting of text. 66 Database php version 2.1 33
  34. 34. 28/06/2009 Retrieving data via URL 1 • Open test_form.php and save as test_form_get.php. • Change the form method to GET GET. • Open test_form_processor.php file and save as test_form_processor_get.php. • Delete the bindings. • Insert 2 new bindings: – the URLVariable URLVariable. – Next slide shows how. 67 Retrieving data via URL 2 Select the URL Variable and the screen below will appear. Put the name of the variable from the page calling this one, as shown; firstName The new binding as shown g will appear. Drag it to where the variable should be shown. The next slide shows this. 68 Database php version 2.1 34
  35. 35. 28/06/2009 Looking at the code • This code is similar to the form request, except the form has change q p g to GET. 69 Sending data with Hyperlinks. • When you send data with forms and a method of GET, information is sent in the URL. URL • Another method of sending the info in the URL is in a Hyperlink. • To do this you can just add the information in a name and info pair to the url of the link. eg: l f th li k – ?firstName=Larry 70 Database php version 2.1 35
  36. 36. 28/06/2009 Sending data with Hyperlinks 2. • 1st page. • Click on a link, eg cat and the page appears like. • Click on dog and the page appears like. 71 Sending data with Hyperlinks 3 • Create a page and save it as animal_question.php and create it to look like thi l k lik this. • The links are to the page animal_home_page.php 72 Database php version 2.1 36
  37. 37. 28/06/2009 Sending data with Hyperlinks 4 • The links on the animal_question.php p g page should look like.. • Cat • Dog g 73 Sending data with Hyperlinks 5 • Another way to set the link with p parameters is, click on the browse icon select the file and click on the parameters buttons as shown. 74 Database php version 2.1 37
  38. 38. 28/06/2009 Sending data with Hyperlinks 6 • The animal_home_page.php should look like. • Create the URL Variable as before with the binding panels. 75 Cookies - 1 • The form and querystring request are stateless variables. Once the server has passed th d them onto th client the server has t the li t th h no memory of them. • Cookies are tiny text files that are written onto the user’s hard drive. • Cookies help to maintain state over coming the t t l th stateless HTTP protocol. t l 76 Database php version 2.1 38
  39. 39. 28/06/2009 Cookies - 2 • Cookies and security – Cookies can’t be used to infect computer p with a virus. – Cookies can only be read by the site that sent them. – Cookies should never contain credit card details, password, etc. ,p , 77 Cookies - 3 • Open the test_form_processor_get.php file . • Create a new page test_form_processor_cookies.php • Add the following to the test_form_processor_get.php file. file Cookie variable name This code must be place before the html tag 78 Database php version 2.1 39
  40. 40. 28/06/2009 Cookies - 4 • Create the bindings and place them in the design view of the g test_form_processor_cookies.php file. • As shown, then save and preview. 79 Lab 1 • Objectives • After completing this lab, you should p g y be able to: – Create URL variables in the Bindings panel. – Display URL variables on a page. – Create FORM variables in the Bindings panel. l – Display FORM variables on a page. 80 Database php version 2.1 40
  41. 41. 28/06/2009 Lab 1 cont-2 • Copy the folder lab_start from the x drive to your working drive. y g • Create a new site called lab • Setup the folders on the IIS root folder and the remote and testing servers. 81 82 unit2sendingpage.php Database php version 2.1 41
  42. 42. 28/06/2009 unit2formactionpage.php 83 unit2urlactionpage.php?firstname=Larry 84 Database php version 2.1 42
  43. 43. 28/06/2009 Chapter 2 Sending form details via emails 85 PHP Sending E-mails • PHP allows you to send e-mails directly from a script. y p • The PHP mail() Function • The PHP mail() function is used to send emails from inside a script. 86 Database php version 2.1 43
  44. 44. 28/06/2009 Syntax Note: For th N t F the mail f il functions t b available, PHP requires an ti to be il bl i installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. Read more in our PHP Mail reference. 87 PHP Simple E-Mail • This can be used to check if the server is setup correctly. p y 88 Database php version 2.1 44
  45. 45. 28/06/2009 Sending Emails - 1 • 1st check that SMTP is installed. Through the window component g p wizard. Highlight IIS and click on details. • If the SMTP is ticked then it is installed if not you need to install it. 89 Sending Emails - 2 • Overview: – In this task you will create a message y g sent page that will transfer the data to an email. – The page that obtains the email data is the contact.php page. You will add a table with no borders and a form to send the information to the message_sent.php page. 90 Database php version 2.1 45
  46. 46. 28/06/2009 Sending Emails - 3 • Open the generic_template.php and save it as message_sent.php g p p • Looks like this • Make it look like this, with link to index.php. 91 Sending Emails – 4 • Add the php code as shown. 92 Database php version 2.1 46
  47. 47. 28/06/2009 Sending Emails - 5 • Open contact.php and change it to look like this. Details on the next slide. 93 Sending Emails - 6 • Position the insertion point below the image caption, and choose Insert > Form > Form to create a new form. • Without moving the insertion point, choose Insert > Table, providing the following settings. Click OK. – Rows: 4 – Columns: 2 – Width: 95 Percent – Border: 0 – Cell Padding: 3 – Cell Spacing: 0 94 Database php version 2.1 47
  48. 48. 28/06/2009 Sending Emails - 7 • Select the <table> tag using the tag selector, and use the Property inspector to change its bgcolor (background color) attribute to #eeeeee, which is a light gray gray. • In the right column of the table insert the following; – Two text fields called, • emailAddress • subject – One text area called body – A Submit button • In the first three cells of the left column, enter the following text: – Your Email Address – Subject – Message Body 95 Sending Emails - 8 • Select the text area and give it the following specifications; – Char Width :55 55 – Num Lines as 6, and Wrap as Virtual. • Select the form in the tag selector, in the Property inspector set: – Name = frm_message – Action = messageSent.php – Method = POST • Insert a Heading 2 element above the table that reads, “Send Us a Message.” 96 Database php version 2.1 48
  49. 49. 28/06/2009 Client-Side Form Validation • Open contact.php • Select the submit subm t button • In the behaviours panels add a behaviour as shown. h 97 Client-Side Form Validation • In the Validate form dialog as shown set the fields as – emailAddress: Required as an email – Subject: Required as anything – Body: Required as anything. • Resulting with the dialog shown below. b l 98 Database php version 2.1 49
  50. 50. 28/06/2009 Client-Side Form Validation • Notice the behaviours p panel should look like this when you highlight the submit button. • Save and test the validation. validation 99 Lab 2 - 1 • Overview: – Create a html form with client-side validation – Create an action page to send an email. • Your overview instructions located in the next section of the book: lab section. section • Your two pages will look like the following 2 slides. 100 Database php version 2.1 50
  51. 51. 28/06/2009 Sendemail.php 101 Sendemail_action.php 102 Database php version 2.1 51
  52. 52. 28/06/2009 Sample of email sent 103 Lab 2 - 1 • Open server_side_scripting site • Open tourtemplate.php. • Change to Split view view. • In the Design view pane, click on the link Or email us. • In the Property inspector change the link to link to sendemail.php. Hit the enter key after putting in the link (if you don’t hit enter, enter the value will not be saved). saved) • Save tourtemplate.php. 104 Database php version 2.1 52
  53. 53. 28/06/2009 Lab 2 - 2 • Create an Email Form Page – Save tourtemplate.php as sendemail.php. – Replace the generic left heading with Send Email. – Replace the generic Left Side Text with the text “Fill in the values in the form and submit the form to send an email to us. Thank you for your interest.” – Create a new form below the text. • Name the form email. • Set the action to sendemail_action.php. • Make sure that the Method is Post. 105 Lab 2 - 3 • Create a table inside the form with 2 columns and 4 rows. – Type in the labels as shown in the table below. • From Address • Subject j • Message Body – In the second column, • first row put a text field and name it from • With a width of 30 and max of 50. – In the second row, • put a text field and name it subject • with the same width and max. – In the third row, • put a textarea control and name it message p g • with a width of 40 and 10 rows. – In the last row, – first column put a submit button control – with a label of Send Email. 106 Database php version 2.1 53
  54. 54. 28/06/2009 Lab 2 – 4 • Create Action Page – Create a new page from tourtemplate.php • name : sendemail_action.php. p p – Note that the email link now points to sendemail.php. • Change the left section title to Email Complete and the text to “Thank you for your email.” • In bindings, create 3 new Request variables: – from – subject – message 107 Lab 2 - 5 • In Code view make space between the heading and the text Type text. the following php code. 108 Database php version 2.1 54
  55. 55. 28/06/2009 Lab 2 - 6 • Replace your email address with mine. The From address would normally be y hard coded with the site’s email address. • Save the page. • Preview the sendemail.php page. Fill out the form and submit it using your t th f d b it i email address as the sender. 109 Lab 2 - 7 • Client Side Validation – Return to Dreamweaver and the sendemail.php page. – Select the Submit button. – In the Behaviours panel, click Add Behaviour and choose Validate Form. – Make all of the fields are required and validate the email address. • Save the page. • Preview sendemail.php. • Check that validation is working by trying to submit an empty form and also try an invalid email address format. 110 Database php version 2.1 55
  56. 56. 28/06/2009 Chapter 3 Self- Self-server application & Server- Server-side validation 111 Self-server Applications • These applications are answering a q question that would usually be y answered by the staff in the tours company. • An example is how much does it cost to do this tour and another tour. • W will create a tours calculator, We ill t t l l t (self-server application). 112 Database php version 2.1 56
  57. 57. 28/06/2009 The tour calculator page - 1 • Open the generic_template.php and save as tourprice_processor.php and p p p p tourprice.php. • The two pages are shown on the next slides. 113 The tour calculator page - 2 Objects names: txtNumberAdults txtNumberChildren cboTourName tourprice.php 114 Database php version 2.1 57
  58. 58. 28/06/2009 The tour calculator page - 3 Calculate link to Contact link to contact.php tourprice.php page tourprice_processor.php 115 The tour calculator page - 4 116 Database php version 2.1 58
  59. 59. 28/06/2009 The tour calculator page - 5 • tourprice.php has the following properties. – Form • Name = frm_tourprice frm tourprice • Action = tourprice_processor.php • Method = POST – Table inside the form • 4 rows • 2 columns • width of 60 percent – List values • Highlights of Argentina, 500 • Highlights of Western Canada, 700 • Egyptian Pyramids and More, 900 117 Calculating the price - 1 • Open tourprice_processor.php page. • Input the php code as shown to; – declare and initialise the number of adults, kids and tour name. – Calculate the price of the tour. Variable declaration Check you have the same variable names in your tourprice.php page as those in quotes. 118 Database php version 2.1 59
  60. 60. 28/06/2009 Calculating the price - 2 • Now you have calculated the tourprice variable, you need to display it. This is done by placing php script amongst the html code as code, shown below. • Save and view the page. What happens when you don’t put in an amount for the adults or kids? 119 The calculator so far…. • So far the calculator works if numeric values are put in the adults p and kids fields. • Errors appear if not values are placed in the adults and kids fields. • The next couple of slides are going to add a css rule f errors and trap dd l for dt the error with php code. 120 Database php version 2.1 60
  61. 61. 28/06/2009 Adding new style to the css • Add an error style to the css file with the following properties; – Type: class – Name: error – Weight : bold – Colour : #990000 • This style will be used if the client makes an error with the information for the tour ith th i f ti f th t calculator. 121 Server-side validation -1 • Server-side validation is validating the tourprice.php page variables sent to the tourprice_processor.php page. • This is done by checking the two form variables for numerical values, as shown. • If one of them doesn’t hold a numeric value the client is sent back to the tourprice.php page, displaying an error. • Add the code code. 122 Database php version 2.1 61
  62. 62. 28/06/2009 Server-side validation- 2 • Once the client is sent back to the tourprice.php page the code to pick up this error is needed needed. • This code shows the response to that variable and displays a message to the client. 123 Lab 3 - 1 • In this lab you are to add a list/menu to reservations.php and change the form action to call the page reservations_action.php. p g p p • The two pages are shown on the next slides. • To have “Yes” or “No” displayed on equipment you will have to write an if statement finding out if the box was ticked, “true” or not ticked, “false”. If request.form(“equipment”)=“true” then response.write(“No”) (“ ) Else response.write(“Yes”) End If 124 Database php version 2.1 62
  63. 63. 28/06/2009 Lab 3 - 2 The value for each label is: 1 for Nepal Track, 2 for Alasken Wilderness, etc. reservations.ph 125 p Lab 3 - 3 Reservation Information from reservation.php page reservations_action.php. This page was created 126 form tourtemplate.php. Database php version 2.1 63
  64. 64. 28/06/2009 Chapter 4 Introduction to Databases in PHP 127 Overview • In this chapter you will : – Create a database connection in dreamweaver. – Create a recordset. – Display data from the recordset. 128 Database php version 2.1 64
  65. 65. 28/06/2009 Placing the database on the server • We are going to using the database y you created in the sql component: q p newlands. • Make sure this is database is in the sql directory as shown below. 129 Exploring the Database • Open the database called newland_tours.mdb. • This is the database that you will be working with. • The relationships look like this. 130 Database php version 2.1 65
  66. 66. 28/06/2009 Connecting the site to a database • Select the + as shown. • The following sql follow ng connection appears, Note the select it. database tag has • This will produce a dialog been selected. as shown. Complete the dialog as shown. Test the connection and click OK 131 Check the Connection • Click on the + and explore the tables etc. • Note a folder has been added to your site with the connection php file. • This file is a Dreamweaver configuration. 132 Database php version 2.1 66
  67. 67. 28/06/2009 What’s in the connection file? 133 Displaying data Using a recordset- 1 • Open the index.php file p p in design view and delete the highlight text. 134 Database php version 2.1 67
  68. 68. 28/06/2009 Displaying data - 2 • Select the traveller’s journal image and right click on the h3 tag in the tag selector. • Select the remove tag from the pop up menu. 135 Displaying data - 3 • Select Creating a recordset the + in the th bindings as shown. • Fill in the dialog as shown. 136 Database php version 2.1 68
  69. 69. 28/06/2009 Displaying data - 4 • Drag and drop description as shown. shown • Looks like • You can also view Data using the live data as shown 137 Lab 4 -1 • Overview – Create sql connection for the lab q database – Create a dreamweaver connection – Create a recordset for the equipment. – Create a dynamic table using the equipment recordset recordset. 138 Database php version 2.1 69
  70. 70. 28/06/2009 Lab 4 equpment.php Design Dynamic table 139 Lab 4 equipment.php live 140 Database php version 2.1 70
  71. 71. 28/06/2009 Lab 4 -2 • Connect the site to the database – call it dwda. • Open equipment.php page. • Create a recordset – Call it rs_equip – Select all field except EquipID – Sort on EquipType field in Ascending order. • Test the recordset. 141 Lab 4- recordset 142 Database php version 2.1 71
  72. 72. 28/06/2009 Lab 4 -3 • Select the HTML table containing equipment and delete the table. • On the Application tab on the Insert bar, click Dynamic Data and choose Dynamic Table. • Choose all records from the rs_equip recordset and give a border of 1 and a cellpadding of 3. • Click OK. • Change the headings to be more descriptive and bold them. • Save the page and preview it. 143 Lab 4 -4 • Format the price column – Select the binding on the page for the price column. – In the Bindings panel, the price column will be g p , p highlighted. Drop down the Format column’s select box and choose Currency > Default. Refer to next slide for menus. • Note: You may not see the Format column in the Bindings panel if it is too narrow. You can expand the panels or scroll within the Bindings panel. l • Preview the page. You should see the same list of equipment now coming from the database. 144 Database php version 2.1 72
  73. 73. 28/06/2009 Formatting numbers. 145 Chapter 5 Dynamical Calculations 146 Database php version 2.1 73

×