html5-img
1 / 52

PHP and MySQL

PHP and MySQL. David Lash Module 2 Working with forms, PHP conditionals and loops. Objectives. Receiving input from HTML forms Review the form tags Receiving form elements Using mail Using register globals Conditional Test statements if statement elseif statement else statement.

tova
Download Presentation

PHP and MySQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Competencies • 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. Creating HTML Input Forms • HTML Forms and 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

  5. Starting And Ending HTML Forms • You can create HTML forms by using the HTML <form> and </form> tags.

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

  7. Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 4.<form action="http://webwizard.aw.com/~phppgm/First.php" method="post" > 5. Click submit to start our initial PHP program. 6. <br> <input type="submit" value="Click To Submit"> 7. <input type="reset" value="Erase and Restart"> 8. </form> 9. </body> </html>

  8. Creating Text boxes and password boxes Text Box

  9. Radio buttons and text areas Radio Buttons Text Area

  10. Creating HTML Input Forms Check Box Select Box

  11. Creating Check Boxes

  12. Competency Alert: You need to know this! Receiving Form Input into PHP Scripts • Calling form: <html> <head><title> testing 1 2 3 </title> </head> <body> <form action=receiveit.php method=post> <input type="radio" name="contact" value="Yes"> Yes <input type="radio" name="contact" value=“No"> No </form></body></html> • The file receiveit.php could contain: <?php $the_contact = $_POST[contact]; print “the contact is $the_contact <br> “; ?> Use contact here and here Since use method=post Receive with $_POST[]

  13. Full Example • Suppose your HTML form uses the following: • Enter email address: <input type="text" size="16" maxlength="20" name="email"> • Enter contact: <input type="text" size="16" maxlength="20" name=“contact"> • Then can receive input as follows: 1. <html> 2. <head><title> Receiving Input </title> </head> 3. <body> • $email = $_POST[email]; • $contact = $_POST[contact]; • <font size=5>Thank You: Got Your Input.</font> 5. <?php 6. print ("<br>Your email address is $email"); 7. print ("<br> Contact preference is $contact"); 9. ?>

  14. Register_Globals? • Since PHP 4.2.1, the default PHP configuration does NOT set a global variable that matches the CGI variable name! (for security reasons) • 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. How can you tell if Register_Globals is OFF? • Enter the following PHP script and run it. • <?PHP phpinfo(); ?> • Search through the output for REGISTER_GLOBALS and see if it is set to OFF or ON. • If it is off you must use the following way to receive input data.

  16. Competency Alert: You need to know this! Getting input data with Register_Globals OFF? • 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.

  17. Full Example, with REGISTER_GLOBALS 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> 4. $email = $_POST[“email”]; 5. $contact = $_POST[“contact”]; 6. <font size=5>Thank You: Got Your Input.</font> 7. <?php 8. print ("<br>Your email address is $email"); 9. print ("<br> Contact preference is $contact"); 10. ?>

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

  19. 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 destination email address. Specify the Text of the email Specify the subject line of the e-mail. 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. More on mail() • To:Multiple email addresses are comma separated • dlash@condor.depaul.edu, gmartin@yahoo.com, gsmith@yahoo.com • Subject • Do not include \n (newline) in subject • Message • Separate each line to send with newline character (\n) • Additional Headers • Includes: From, Cc, and Bcc. • Multiple extra headers separates with "\r\n" • For example, • <?php$to = ‘elwood@blues.com, jake@blues.com’;$subject = ‘Testing 1 2 3';$message = ‘Hello: \n This is a 2 line message.';$headers = 'From: george@george.com’ . "\r\n" .‘Bcc: ‘TheBoss@yahoo.com.com’ . "\r\n“;mail($to, $subject, $message, $headers);?>

  21. So you can • 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: • <html> • <head><title> Receiving Input </title> </head> • <body> • <?php • $email = $_POST[email]; • $contact = $_POST[contact]; • Print ‘<font size=5>Thank You: Got Your Input.</font>’; • print ("<br>Your email address is $email"); • print ("<br> Contact preference is $contact"); • $dest='orders@hardwareville.com'; • $subject = 'Received contact info'; • $message = 'Enclosed is contact info for email=$email.'; • $message .= "contact info = $count "; • $extra = 'From: harry@hardwareville.com'; • mail( $dest, $subject, $message, $extra ); • ?>

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

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

  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. Test Expressions • Use test operatorswithin test expressions. • Test operators evaluate to true or false

  26. A Full Example ... • 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. Receiving Code (With REGISTER_GLOBALS off) 1. <html> 2. <head><title>Decisions</title></head> 3. <body> 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! <br>"; 8. } 9. $max=$grade1; 10. if ($grade1 < $grade2) { 11. $max = $grade2; 12. } 13. print ("Your max score was $max"); 14. ?> 15. </body></html> Get grade1 and grade2 from HTML form. Calculate average Output if $average is more than 89 Set when $grade2 is more than $grade1

  28. Using empty() • 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 “</body></html>”; exit; } $name = $_POST[name]; print “got your name=$name”; ?> If variable ‘name’ is empty then Let user know and end.

  29. So for example,

  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. Comparing Strings • 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. Comparing Strings • Also can use <, >, <=, and >= operators to compare string values using ASCII code values. • 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. Using the elseif Clause • 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 } • The above script checks the elseif test expression when the test condition for the if statement is false.

  34. Using the elseif Clause • 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 condition is false. Check this test expression when the first two conditions are all false. Check this test expression when the first three conditions are all 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. Using the else Clause • 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. 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. A Full Example ... • 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 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 $grade2

  38. Receiving Code (with REGISTER_GLOBALS Off) Get values of $grade1 and $grade2 1. <html> <head><title>Grade Calculation</title></head> 2. <body> 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 ("<br>Your max score was $max"); 24. ?> </body></html> Compute average of $grade1 and $grade2 Check if $average is an “A”, “B”,”C”, “D” or “F”

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

  40. Using Logical Test Operators • 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. Logical Test Operators • 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. 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. 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 statementis true when $flag is anything except 0.

  44. Consider the following example ... • Example asks the user to guess a “secret” two-digit combination, uses logical test operators. • The Input HTML form uses the following to set pick1. A similar group sets a variable pick2. <font size=4 > Pick a number from 1 to 9 <br> <input type="radio" name="pick1" value="1">1 <input type="radio" name="pick1" value="2">2 <input type="radio" name="pick1" value="3">3 <input type="radio" name="pick1" value="4">4 <input type="radio" name="pick1" value="5">5 <input type="radio" name="pick1" value="6">6 <input type="radio" name="pick1" value="7">7 <input type="radio" name="pick1" value="8">8 <input type="radio" name="pick1" value="9">9

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

  46. Front-end Survey . . . <html> <head><title> Simple Quiz </title></head> <body> <form action=grade.php method=post> <OL><LI> Who was the first president of the United states </LI> <OL start=1 type=a> <li> <input type=radio name=president value=johnson> Johnson <li> <input type=radio name=president value=kennedy> Kennedy <li> <input type=radio name=president value=adams> Adams <li> <input type=radio name=president value=washington> Washington </OL> <LI> Who was the first vice-president of the United states </LI> <OL start=1 type=a> <li> <input type=radio name=vp value=johnson> Johnson <li> <input type=radio name=vp value=kennedy> Kennedy <li> <input type=radio name=vp value=adams> Adams <li> <input type=radio name=vp value=washington> Washington </OL> <input type=submit> </form> </body>

  47. One Possible Solution . . . <html><head><title>Title here!</title></head><body> <?php $right=0; if ( empty( $_POST['president'] ) || empty( $_POST['vp'] ) ) { print "Please answer question 1 and 2"; print "</body></html>"; 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 <br>"; print "The question numbers correct are: $correct <br>"; ?></body></html>

  48. PHP Code 1. <html><head><title>Number Guess Results </title><head> 2. <body> 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. ?></body></html>

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

  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.

More Related