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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

5.04 Apply Decision Making Structures
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
David Lash DePaul University SNL 262 Web Page Design Forms! - Chapt 7 Instructor: David A. Lash.
1 *Copyright © 2002 Pearson Education, Inc.. 2 Web Wizard’s Guide to CGI/Perl David Lash Chapter 3 Perl Basics.
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow.
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
Python and Web Programming
CIS101 Introduction to Computing Week 12 Spring 2004.
David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 2 Instructor: David A. Lash Enhancing HTML With PHP.
Principles of Web Design 6 th Edition Chapter 11 – Web Forms.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
1 Web Developer & Design Foundations with XHTML Chapter 6 Key Concepts.
9/4/2015CS346 PHP1 CHAPTER 2 Using Variables. 9/4/2015CS346 PHP2 Objectives  How to store and access data in PHP variables  How to create and manipulate.
Web forms in PHP Forms Recap  Way of allowing user interaction  Allows users to input data that can then be processed by a program / stored in a back-end.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
4-Sep-15 HTML Forms Mrs. Goins Web Design Class. Parts of a Web Form A Form is an area that can contain Form Control/Elements. Each piece of information.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
1 Creating Web Forms in HTML Web forms collect information from customers Web forms include different control elements including: –Input boxes –Selection.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
XHTML Introductory1 Forms Chapter 7. XHTML Introductory2 Objectives In this chapter, you will: Study elements Learn about input fields Use the element.
Robinson_CIS_285_2005 HTML FORMS CIS 285 Winter_2005 Instructor: Mary Robinson.
Chapter 2 - Algorithms and Design
Working with Forms. How Forms Work Forms let you build interactive Web pages that collect information from a user and process it on the Web server The.
NMD202 Web Scripting Week3. What we will cover today Includes Exercises PHP Forms Exercises Server side validation Exercises.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Slide 7-1 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
Slide 3-1 CHAPTER 3 Conditional Statements Objectives To learn to use conditional test statements to compare numerical and string data values To learn.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
CSC 2720 Building Web Applications Server-side Scripting with PHP.
XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML.
1 Module 3 Conditional Statements. Objectives  Conditional test statements to compare numerical and string data values  Looping statements to repeat.
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
ITM © Port, Kazman1 ITM 352 More on Forms Processing.
JavaScript, Fourth Edition
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
Web Page Design Forms! Website Design. Objectives What forms can do The Attributes of the form tag Using Textboxes Textareas Checkboxes Radio buttons.
©SoftMooreSlide 1 Introduction to HTML: Forms ©SoftMooreSlide 2 Forms Forms provide a simple mechanism for collecting user data and submitting it to.
1 Web Wizards Guide To PHP David Lash Chapter 2 Using Variables.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 13.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Introduction to Programming the WWW I CMSC Winter 2004 Lecture 8.
Receiving form Variables
Chapter 4 C Program Control Part I
IF statements.
More Selections BIS1523 – Lecture 9.
Conditions and Ifs BIS1523 – Lecture 8.
The Web Wizard’s Guide to PHP
T. Jumana Abu Shmais – AOU - Riyadh
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Making decisions with code
The Web Wizard’s Guide to PHP by David A. Lash
Presentation transcript:

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

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 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 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 Starting And Ending HTML Forms You can create HTML forms by using the HTML and tags.

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 Another Full Script Example A Simple Form 3. 4.<form action=" method="post" > 5. Click submit to start our initial PHP program

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 Receiving Form Input into PHP Scripts l Calling form: testing 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 Full Example l Suppose your HTML form uses the following: »Enter address: »Enter contact: Then can receive input as follows: Receiving Input $ = $_POST[ ]; 5. $contact = $_POST[contact]; 6. Thank You: Got Your Input. 5.<?php 6. print (" Your address is $ "); 7. print (" Contact preference is $contact"); 9. ?>

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

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 Sending from PHP scripts l Sometimes it is useful to send from a PHP script: »PHP uses mail() that by default sends via the Simple Mail Transfer Protocol (SMTP). »mail(to_address, subject, message, extra_headers); Specify the destination address. Specify the subject line of the . Specify the Text of the Specify additional headers $subject = 'New Hardware Order'; 3. $message = 'Enclosed is a new order for 12 hammers.\n Thanks.'; 4. $extra = 'From: 5. mail( $dest, $subject, $message, $extra ); Competency Alert: You need to know this !

20 More on mail() l To: Multiple addresses are comma separated 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 So you can l Suppose your HTML form uses the following: »Enter address: Then can receive input as follows: Receiving Input <?php 5. $ = $_POST[ ]; 6. $contact = $_POST[contact]; 7. Print ‘ Thank You: Got Your Input. ’; 8. print (" Your address is $ "); 9. print (" Contact preference is $contact"); $subject = 'Received contact info'; 12. $message = 'Enclosed is contact info for =$ .'; 13. $message.= "contact info = $count "; 14. $extra = 'From: 15. mail( $dest, $subject, $message, $extra ); 16. ?>

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 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 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 operators within test expressions. »Test operators evaluate to true or false

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 Receiving Code (With REGISTER_GLOBALS off) 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 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 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 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 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 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 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 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 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... 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 Receiving Code (with REGISTER_GLOBALS Off) 1. Grade Calculation <?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 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 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 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 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 statement is true when $flag is anything except 0.

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 Consider the following example...

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

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 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 PHP Code 1. Number Guess Results <?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 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 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 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 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 "; ?>