1 php and mysql david lash module 2 working with forms, php conditionals and loops

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

Post on 22-Dec-2015

226 views

Category:

Documents


3 download

TRANSCRIPT

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

1

PHP and MySQL

David Lash

Module 2

Working with forms,

PHP conditionals and loops

Page 2: 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

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

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

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

4

HTML Forms and not part of PHP language but important way to send data to scripts

Creating HTML Input Forms

Text BoxRadio Buttons

Check Box

Select Box

Text Area

Submit/Reset button

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

5

Starting And Ending HTML Forms

<form action="http://webwizard.aw.com/~phppgm/program.php" method="post">

Program to start when form is submitted.

Place form elements between<form> and </form> tags.

.

.

.</form>

Format tosend data.

Forms end with </form>

You can create HTML forms by using the HTML <form> and </form> tags.

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

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”.

<input type=”submit” value=”Click To Submit”> <input type=”reset” value=”Erase and Restart”>

Type ofbutton to create Button Label

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

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>

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

8

Creating Text boxes and password boxes

Text Box

Name: <input type="text" size="15" maxlength="20" name="fname">

Maximum number of inputcharacters

The width of text box. Use this name to identify the formelement in the receiving program.

Create a text box.

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

9

Radio buttons and text areas

<textarea rows="4" cols="50" name="Comments">Your comments here</textarea>

Number of columns.Number ofrows

Text areas haveclosing tags.

Any text here will appear asdefault text in text area.

Radio Buttons

Text Area

The value that will be sent tothe form-processing program.

Since both radio buttons have the same name,the radio buttons will operator together.

This item will bepre-checked whenthe form is viewed.

Create radio button.

<input type=”radio” name=”contact” value=”Yes” checked> <input type=”radio” name=”contact” value=”No” >

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

10

Creating HTML Input Forms

Check Box

Select Box

Allows end-user toselect multiple items.

Viewable window size

<select name="Accommodations" size=2 multiple> <option> A fine hotel </option> <option selected> A cheap motel! </option> <option> A tent in the parking lot </option> <option> Just give me a sleeping bag checked </option> </select>

Variable name set inthe receiving script.

This text is displayed as an option and the entire textwill be returned as the variable's value if selected.

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

11

Creating Check Boxes

The value that will be sent to theform-processing program.Each check box sets a different

variable name when selected.

This item will be pre-checkedwhen the form is viewed.

Createcheckbox

<input type=”checkbox” name=”walk” value=”Yes” checked> Walk <input type=”checkbox” name=”Bicycle” value=”Yes”> Bicycle <input type=”checkbox” name=”Car” value=”Yes”> Car <input type=”checkbox” name=”Plane” value=”Yes”> Plane

The value that will be sent tothe form-processing program.

Since each checkbox element has the same name,multiple values can be set for the same variable name.

This item will be pre-checkedwhen form is viewed.

Createcheckbox

<input type=”checkbox” name=”travel” value=”Car” checked> Car? <input type=”checkbox” name=”travel” value=”Bike”> Bicycle? <input type=”checkbox” name=”travel” value=”Horse”> Horse? <input type=”checkbox” name=”travel” value=”None”> None of the above?

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

12

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> “;

?>

Competency Alert:You need to

know this!

Since use method=postReceive with $_POST[]

Use contact here and here

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

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>4. $email = $_POST[email];5. $contact = $_POST[contact];6. <font size=5>Thank You: Got Your Input.</font>5. <?php6. print ("<br>Your email address is $email");7. print ("<br> Contact preference is $contact");

9. ?>

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

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

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

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.

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

16

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 squarebracket 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!

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

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. <?php8. print ("<br>Your email address is $email");9. print ("<br> Contact preference is $contact");

10. ?>

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

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

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

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 subject line of the e-mail.

Specify the Text of the email

Specify additional email headers.

1. $dest='[email protected]';2. $subject = 'New Hardware Order';3. $message = 'Enclosed is a new order for 12 hammers.\n Thanks.';4. $extra = 'From: [email protected]';5. mail( $dest, $subject, $message, $extra );

Competency Alert:You need to

know this!

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

20

More on mail() To: Multiple email addresses are comma separated

» [email protected], [email protected], [email protected] 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 = ‘[email protected], [email protected]’;$subject = ‘Testing 1 2 3';$message = ‘Hello: \n This is a 2 line message.';$headers = 'From: [email protected]’ . "\r\n" .‘Bcc: ‘[email protected]’ . "\r\n“;mail($to, $subject, $message, $headers);

?>

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

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:1. <html>2. <head><title> Receiving Input </title> </head>3. <body> 4. <?php5. $email = $_POST[email];6. $contact = $_POST[contact];7. Print ‘<font size=5>Thank You: Got Your Input.</font>’;

8. print ("<br>Your email address is $email");9. print ("<br> Contact preference is $contact"); 10. $dest='[email protected]';

11. $subject = 'Received contact info';

12. $message = 'Enclosed is contact info for email=$email.';

13. $message .= "contact info = $count ";

14. $extra = 'From: [email protected]';

15. mail( $dest, $subject, $message, $extra );

16. ?>

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

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

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

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.

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

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.

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

25

Test Expressions Use test operators within test expressions.

» Test operators evaluate to true or false

Test Operator

Effect Example Result

== Equal to if ($x == 6){ $x = $y + 1; $y = $x + 1; }

Run the second and third

statements if the value of $x is equal to 6.

!= Not equal to if ($x != $y) { $x = 5 + 1; }

Run the second statement if the value of $x is not equal to the value of $y.

< Less than if ($x < 100) { $y = 5; }

Run the second statement if the value of $x is less than 100.

> Greater than if ($x > 51) { print "OK"; }

Run the second statement if the value of $x is greater than 51.

>= Greater than or equal to

if (16 >= $x) { print "x=$x"; }

Run the second statement if 16 is greater than or equal to the value of $x.

<= Less than or equal to

if ($x <= $y) { print "y=$y"; print "x=$x"; }

Run the second and third statements if the value of $x is less than or equal to the value of $y.

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

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

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

27

Receiving Code (With REGISTER_GLOBALS off)

1. <html>2. <head><title>Decisions</title></head>3. <body>4. <?php5. $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 grade2from HTML form.

Calculate average

Output if $average is more than 89

Set when $grade2 is more than $grade1

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

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’ isempty thenLet user know and end.

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

29

So for example,

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

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”

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

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”.

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

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”.

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

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.

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

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 testexpression when thefirst three conditionsare all false.

Check this testexpression when thefirst two conditionsare all false.

Check this testexpression when thefirst 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.

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

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

}

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

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");

}

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

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

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">

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

38

Receiving Code (with REGISTER_GLOBALS Off)

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 $averageis an “A”, “B”,”C”, “D”or “F”

Get values of $grade1 and $grade2

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

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

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

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) {

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

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.

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

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.

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

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.

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

44

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

Consider the following example ...

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

45

Write a Survey Application that

Score the survey, report the number correct. Indicate questions correct.

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

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>

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

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>

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

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>

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

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

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

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.

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

51

Front –end HTML 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></html>

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

52

One Possible Solution<html><head> <title>Title here!</title></head><body><?php $right=0; if ( !isset( $_POST['president'] ) || !isset( $_POST['vp'] ) ) {

print "Please answer question 1 and 2";print "</body></html>";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 <br>";print "The question numbers correct are: $correct <br>";?></body></html>