Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops.

Similar presentations


Presentation on theme: "1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops."— Presentation transcript:

1 1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops

2 2 Objectives l Receiving input from HTML forms »Review the form tags »Receiving form elements »Using mail »Using register globals l Conditional Test statements »if statement »elseif statement »else statement

3 3 Competencies l At end of unit be able to write scripts that »Receive input from forms »Use conditional branches Competency Alert: You need to know this ! Common Problem Area! People seem to forget this

4 4 l HTML Forms and not part of PHP language but important way to send data to scripts Creating HTML Input Forms Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button

5 5 Starting And Ending HTML Forms You can create HTML forms by using the HTML and tags.

6 6 Creating Form Buttons l You can create submit and reset buttons by placing the following within & tags. l The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase and Restart”.

7 7 Another Full Script Example 1. 2. A Simple Form 3. 4.<form action="http://webwizard.aw.com/~phppgm/First.php" method="post" > 5. Click submit to start our initial PHP program. 6. 7. 8. 9.

8 8 Creating Text boxes and password boxes Text Box

9 9 Radio buttons and text areas Radio Buttons Text Area

10 10 Creating HTML Input Forms Check Box Select Box

11 11 Creating Check Boxes

12 12 Receiving Form Input into PHP Scripts l Calling form: testing 1 2 3 Yes No l The file receiveit.php could contain: <?php $the_contact = $_POST[contact]; print “the contact is $the_contact “; ?> Competency Alert: You need to know this ! Since use method=post Receive with $_POST[] Use contact here and here

13 13 Full Example l Suppose your HTML form uses the following: »Enter email address: »Enter contact: Then can receive input as follows: 1. 2. Receiving Input 3. 4. $email = $_POST[email]; 5. $contact = $_POST[contact]; 6. Thank You: Got Your Input. 5.<?php 6. print (" Your email address is $email"); 7. print (" Contact preference is $contact"); 9. ?>

14 14 Register_Globals? l Since PHP 4.2.1, the default PHP configuration does NOT set a global variable that matches the CGI variable name! (for security reasons) l If your site has REGISTER_GLOBALS OFF you must use a different mechanism to receive HTML Form Variables. Technical details: The apache admin can set REGISTER_GLOBALS OFF (new default) or ON in the php.ini configuration file. Why do you care? Running with REGISTER_GLOBALS ON, can be a HUGE security risk) E.g., if ( is_privildged() ) { super_user = TRUE; } Someone could call your script as: yourscript.php?super_user=1

15 15 How can you tell if Register_Globals is OFF? l Enter the following PHP script and run it. » l Search through the output for REGISTER_GLOBALS and see if it is set to OFF or ON. l If it is off you must use the following way to receive input data.

16 16 Getting input data with Register_Globals OFF? l To receive data with REGISTER_GOBALS OFF you use a special variable called $_POST. »$mynm = $_POST[“name”]; Enclose in square bracket and then quotes Name of HTML form CGI variable (note do not use $) Special PHP Global variable. Technically it is an associative array (covered in chptr 5.) PHP variable name that you want to receive the HTML form input. Competency Alert: You need to know this !

17 17 Full Example, with REGISTER_GLOBALS OFF. l Suppose your HTML form uses the following: »Enter email address: Then can receive input as follows: 1. 2. Receiving Input 3. 4. $email = $_POST[“email”]; 5. $contact = $_POST[“contact”]; 6. Thank You: Got Your Input. 7.<?php 8. print (" Your email address is $email"); 9. print (" Contact preference is $contact"); 10. ?>

18 18 Objectives l Receiving input from HTML forms »Review the form tags »Receiving form elements »Using mail »Using register globals l Conditional Test statements »if statement »elseif statement »else statement

19 19 Sending email from PHP scripts l 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 destination email address. Specify the subject line of the e-mail. Specify the Text of the email Specify additional email headers. 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 ); Competency Alert: You need to know this !

20 20 More on mail() l To: Multiple email addresses are comma separated »dlash@condor.depaul.edu, gmartin@yahoo.com, gsmith@yahoo.comdlash@condor.depaul.edugmartin@yahoo.comgsmith@yahoo.com l Subject »Do not include \n (newline) in subject l Message »Separate each line to send with newline character (\n) l Additional Headers »Includes: From, Cc, and Bcc. »Multiple extra headers separates with "\r\n" l For example, »

21 21 So you can l Suppose your HTML form uses the following: »Enter email address: Then can receive input as follows: 1. 2. Receiving Input 3. 4. <?php 5. $email = $_POST[email]; 6. $contact = $_POST[contact]; 7. Print ‘ Thank You: Got Your Input. ’; 8. print (" Your email address is $email"); 9. print (" Contact preference is $contact"); 10. $dest='orders@hardwareville.com'; 11. $subject = 'Received contact info'; 12. $message = 'Enclosed is contact info for email=$email.'; 13. $message.= "contact info = $count "; 14. $extra = 'From: harry@hardwareville.com'; 15. mail( $dest, $subject, $message, $extra ); 16. ?>

22 22 Objectives l Receiving input from HTML forms »Review the form tags »Receiving form elements »Using mail »Using register globals l Conditional Test statements »if statement »elseif statement »else statement

23 23 Using Conditional Test Statements l Conditional statements provide a way for scripts to test for certain data values and then to react differently depending on the value found. l Will examine »the if statement, »the elseif clause, »the else clause, »and the switch statement.

24 24 Using the if Statement Use an if statement to specify a test condition and a set of statements to run when a test condition is true. if ($average > 69) { $Grade="Pass"; print "Grade=$Grade "; } print "Your average was $average"; if $average was equal to 70 then the above would output: Your average was 70 When $average is greater than 69 execute these statements.

25 25 Test Expressions Use test operators within test expressions. »Test operators evaluate to true or false

26 26 A Full Example... l Consider the following application: »Receives two grades as input and determines whether their average is above 89. » It uses an HTML form for input grades: Enter First Score <input type="text" size="4” maxlength="7" name="grade1"> Enter Second Score <input type="text" size="4” maxlength="7" name="grade2"> Sets $grade1 Sets $grade2

27 27 Receiving Code (With REGISTER_GLOBALS off) 1. 2. Decisions 3. 4.<?php 5. $grade1= $POST[“grade1”]; 6. $grade2= $POST[“grade2”]; 5. $average = ($grade1 + $grade2) / 2; 6. if ( $average > 89 ) { 7. print "Average score: $average You got an A! "; 8. } 9. $max=$grade1; 10. if ($grade1 < $grade2) { 11. $max = $grade2; 12. } 13. print ("Your max score was $max"); 14. ?> 15. Get grade1 and grade2 from HTML form. Calculate average Output if $average is more than 89 Set when $grade2 is more than $grade1

28 28 Using empty() l Can also use simple functions to test if incoming variable has a value: <?php if ( empty( $_POST[name] ) { print “you must specify a name”; print “ ”; exit; } $name = $_POST[name]; print “got your name=$name”; ?> If variable ‘name’ is empty then Let user know and end.

29 29 So for example,

30 30 Comparing Strings PHP represents strings using the ASCII (“ask-ee”) code values.( American Standard Code for Information Interchange). »ASCII provides a standard, numerical way to represent characters on a computer. »Every letter, number, and symbol is translated into a code number. –“A” is ASCII code 65, “B” is 66, “C” is 67, and so on. –Lowercase “a” is ASCII code 97, “b” is 98, “c” is 99, and s –ASCII “A” is less than ASCII “a,” “B” is less than “b,” and “c” is less than “d”. –ASCII characters have ASCII code values lower than letters. So ASCII character “1” is less than “a” or “A”

31 31 Comparing Strings l You can use == operator to check if one string is equal to another. For example, $name1 = "George"; $name2 = "Martha"; if ($name1 == $name2) { print ("$name1 is equal to $name2" ); } else { print ("$name1 is not equal to $name2"); } Would output: “ George is not equal to Martha ”.

32 32 Comparing Strings l Also can use, = operators to compare string values using ASCII code values. l For Example $name1 = "George"; $name2 = "Martha"; if ($name1 < $name2) { print ("$name1 is less than $name2"); } else { print ("$name1 is not less than $name2"); } It would output “ George is less than Martha ”.

33 33 Using the elseif Clause l Use an elseif clause with an if statement to specify an additional test condition if (test expression) { one or more PHP statements } elseif (test expression) one or more PHP statements } l The above script checks the elseif test expression when the test condition for the if statement is false.

34 34 Using the elseif Clause l One or more elseif clauses can be used with an if statement. if ($hour < 9) { print "Sorry, it is too early."; } elseif ($hour < 12) { print "Good morning. The hour is $hour. "; print "How can we help you?"; } elseif ($hour < 13) { print "Sorry, we are out to lunch. "; } elseif ($hour < 17) { print "Good afternoon. The hour is $hour. "; print "How can we help you?"; } elseif ($hour <= 23) { print "Sorry, we have gone home already."; } Check this test expression when the first three conditions are all false. Check this test expression when the first two conditions are all false. Check this test expression when the first condition is false. if $hour == 15, output “Good afternoon. The hour is 15. How can we help you?” if $hour == 24, then this code outputs nothing.

35 35 Using the else Clause l Use an else clause with if and possibly one or more elseif clauses »Specify set of statements to run when all the previous test conditions are false. »Has the following general format shown in the if (test expression) { one or more PHP statements } else { one or more PHP statements }

36 36 Using the else Clause For example, if $count had a value of –75, then this code would output “Illegal value for count = – 75” if ( $count == 0 ) { print ("Time to reorder."); $reorder=1; } elseif ( $count == 1 ) { $reorder=1; print ("Warning: we need to start reordering."); } elseif ( $count > 1 ) { $reorder = 0; print ("We are OK for now."); } else { print ("Illegal value for count = $count"); }

37 37 A Full Example... l Full example that extends the grade- averaging to determine a letter grade (A, B, C, D, or F) and to catch illegal input. Use the following HTML form for input Sets $grade1 Sets $grade2 Enter First Score <input type="text" size="4” maxlength="7" name="grade1"> Enter Second Score <input type="text” size="4” maxlength="7" name="grade2">

38 38 Receiving Code (with REGISTER_GLOBALS Off) 1. Grade Calculation 2. 3. <?php 4. $grade1 = $_POST[“grade1”]; $grade2 = $_POST[“grade2”]; 5. $average = ($grade1 + $grade2) / 2; 6. if ($average > 89) { 7. print ("Average=$average You got an A"); 8. } elseif ($average > 79) { 9. print ("Average=$average You got a B"); 10. } elseif ($average > 69) { 11. print ("Average=$average You got a C"); 12. } elseif ($average > 59) { 13. print ("Average=$average You got a D"); 14. } elseif ($average >= 0) { 15. print ("Grade=$grade You got an F"); 16. } else { 17. print ("Illegal average less than 0 average=$average"); 18. } 19. $max=$grade1; 20. if ($grade1 < $grade2) { 21. $max = $grade2; 22. } 23. print (" Your max score was $max"); 24. ?> Compute average of $grade1 and $grade2 Check if $average is an “A”, “B”,”C”, “D” or “F” Get values of $grade1 and $grade2

39 39 Objectives l Receiving input from HTML forms »Review the form tags »Receiving form elements »Using mail »Using register globals l Conditional Test statements »if statement »elseif statement »else statement

40 40 Using Logical Test Operators l PHP supports a set of logical test operators you can use to create compound test expressions »used within an if statement or a while statement to specify more than one test condition. »For example, consider the following line –while ($x > $max && $found != 1) {

41 41 Logical Test Operators l PHP supports three logical test operators. 1. &&—the AND operator. Use in if statements and while loops. Example: while ($ctr < $max && $flag == 0) { Whenever either of these expressions is false, the loop will terminate.

42 42 Or operator 2. ||—the OR operator. Used much like the AND operator in if statements andwhile loops. Example, »if ($ctr != $max || $flag == 0) { Carries out the statements within the if statement if either $ctr is not equal to $max or $flag is equal to 0.

43 43 Not operator 3. !—the NOT operator. Used to test whether an expression is false (used in while loops and in if statements). Example, »if (!$flag == 0) { This statement is true when $flag is anything except 0.

44 44 l Example asks the user to guess a “secret” two- digit combination, uses logical test operators. l The Input HTML form uses the following to set pick1. A similar group sets a variable pick2. Pick a number from 1 to 9 1 2 3 4 5 6 7 8 9 Consider the following example...

45 45 Write a Survey Application that Score the survey, report the number correct. Indicate questions correct.

46 46 Front-end Survey... Simple Quiz Who was the first president of the United states Johnson Kennedy Adams Washington Who was the first vice-president of the United states Johnson Kennedy Adams Washington

47 47 One Possible Solution... Title here! <?php $right=0; if ( empty( $_POST['president'] ) || empty( $_POST['vp'] ) ) { print "Please answer question 1 and 2"; print " "; exit; } $pres = $_POST[president]; if ( $pres == 'washington' ){ $right = $right + 1; $correct = "1 "; } $vp = $_POST[vp]; if ( $vp == 'adams' ){ $right = $right + 1; $correct = “$correct 2 "; } print "You got $right questions correct "; print "The question numbers correct are: $correct "; ?>

48 48 PHP Code 1. Number Guess Results 2. 3. <?php 4. $pick1 =$_POST[“pick1”]; $pick2 =$_POST[“pick2”]; 5. $combo1=5; 6. $combo2=6; 7. if (($pick1 == $combo1) && ($pick2 == $combo2)) { 8. print ("Congratulations you got both secret numbers $combo1 $combo2!"); 9. } elseif (($pick1 == $combo1) || ($pick2 == $combo2)){ 10. print ("You got one number right."); 11. } else { 12. print ("Sorry, you are totally wrong!"); 13. } 14. print ("You guessed $pick1 and $pick2."); 15. ?>

49 49 Summary l Receiving input from HTML forms »Review the form tags »Receiving form elements »Using mail »Using register globals l Conditional Test statements »if statement »elseif statement »else statement

50 50 Lab 1 – Create a Survey I have put an answer here. Change the questions to your own. That is, don’t use these questions make up your own.

51 51 Front –end HTML Survey Simple Quiz Who was the first president of the United states Johnson Kennedy Adams Washington Who was the first vice-president of the United states Johnson Kennedy Adams Washington

52 52 One Possible Solution Title here! <?php $right=0; if ( !isset( $_POST['president'] ) || !isset( $_POST['vp'] ) ) { print "Please answer question 1 and 2"; print " "; exit; } $pres = $_POST[president]; if ( $pres == 'washington' ){ $right += 1; $correct.= "1 "; } $vp = $_POST[vp]; if ( $vp == 'adams' ){ $right += 1; $correct.= "2 "; } print "You got $right questions correct "; print "The question numbers correct are: $correct "; ?>


Download ppt "1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops."

Similar presentations


Ads by Google