Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 2 Instructor: David A. Lash Enhancing HTML With PHP.

Similar presentations


Presentation on theme: "David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 2 Instructor: David A. Lash Enhancing HTML With PHP."— Presentation transcript:

1 David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 2 Instructor: David A. Lash Enhancing HTML With PHP

2 David Lash 2 Objectives zTo learn how to pass data from HTML forms to PHP scripts yLearn how to access PHP variables yLearn to manipulate numeric and string variables yUsing these variables to receive HTML form data

3 David Lash 3 PHP Where are we? zSo far we learned that yPHP enhances HTML (probably not sure how) yPHP uses tags yPHP can be mixed in with HTML yPHP uses a print statement yPHP runs before HTML page is returned and interpreted by browser yWhat good is that? My title Well hello out there <?php print “I said Hello”; ?>

4 David Lash 4 Putting labels on data w/ variables zWhen you receive form data, you need some way to label the data. yFor example, when said x yYour saying call whatever the type mystuff zYou can give data names in PHP too  A variable name is a label used within a script to refer to the data. NOTE! Don’t worry even though it’s a variable we are not doing “Math” Here we are setting arbitrary names and values for variables within PHP

5 David Lash 5 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.

6 David Lash 6 Think about them this way $days = 3; $newdays = 100; $days = $newdays; 3100 $days$newdays Put 3 -> $days Put 100 -> $newdays Put whatever is in $newdays Into $days

7 David Lash 7 Selecting Variable Names  Almost any character set but must:  Use a dollar sign ($) as the first character  Use a letter or an underscore character (_) as the second character. Note:Try to select variable names that help describe their function. For example $counter is more descriptive than $c or $ctr. Note2: You cannot use spaces in variable names, so $happy day is not good but $happy_day is.

8 David Lash 8 Using Variables and print  That is, to print out the value of $x, write the following PHP statement:  print ("$x"); For example: $age=6; print ("Bryant is $age years old."); zWould output: Bryant is 6 years old. 6 $bryant

9 David Lash 9 A Full Example... 1. 2. Variable Example 3. 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 second_num=$second_num"); 11. ?> 12 $first_num 356 $second_num $temp

10 David Lash 10 Another way to think about this 1. 2. Variable Example 3. 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 second_num=$second_num"); 11. ?> $first_num$second_num$temp 12 356 12 356 12

11 David Lash 11 Objectives zTo learn how to store and access data in PHP variables zTo understand how to create and manipulate numeric and string variables zTo learn how to pass data from HTML forms to PHP scripts

12 David Lash 12 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.”

13 David Lash 13 Common PHP Numeric Operators

14 David Lash 14 A Full Example 1. 2. Variable Example 3. 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 "); 16. print ("Total Revenue is $total_revenue "); 17. print ("Total Profit is $profit"); 18. ?>

15 David Lash 15 A Full Example... See http://webwizard.aw.com/~phppgm/C2/numops.php http://webwizard.aw.com/~phppgm/C2/numops.php 1. 2. Variable Example 3. 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 "); 16. print ("Total Revenue is $total_revenue "); 17. print ("Total Profit is $profit"); 18. ?>

16 David Lash 16 What happens if … If use a variable without assigning a value to it? Ans: It will have no value (called a null value). If use a null value in an expression may not get an error Instead PHP will try to complete the expression evaluation. For example, will output x= y=4. <?php $y = 3; $y=$y + $x + 1; print ("x=$x y=$y"); ?> $x has null value uses 0

17 David Lash 17 OK, we got to do a little math zJust like 5 th grade math … zNeed Operator precedence rules  define the order in which the operators are evaluated. For example, $ x = 5 + 2 * 6;  The value of $ x is either 42 or 17 depending on order of evaluation.  Since multiplication evaluated before addition operations, this expression evaluates to 17.

18 David Lash 18 PHP Precedence Rules zPHP follows the precedence rules listed below. y First it evaluates operators within parentheses. y Next it evaluates multiplication and division operators. y Finally it evaluates addition and subtraction operators.  For example, the first two statements evaluate to 80 while the last to 180.  $x = 100 - 10 * 2;  $y = 100 - (10 * 2);  $z = (100 - 10) * 2;

19 David Lash 19 A Full Example 1. 2. Expression Example 3. 4. <?php 5. $grade1 = 50; 6. $grade2 = 100; 7. $grade3 = 75; 8. $average = ($grade1 + $grade2 + $grade3) / 3; 9. print ("The average is $average"); 10. ?> Note: This example may not seem like much … but suppose the values of $grade1, $grade2, $grade3 came from an input form (instead of being set here).

20 David Lash 20 Wait … Why am I learning these Variables? zDon’t worry, this is not a “math” class y Later will so you how to use these variables to “receive” input from HTML forms. y For now just looking at how to manipulate variables with pre-defined values y We looked at using variables to hold numbers…  Variables can hold strings of characters too … Note: Why do you want to hold character strings? Consider input from a text box or textarea? Input to your program is like to be characters (not numbers). E.g., their comments

21 David Lash 21 Working with PHP String Variables zCharacter strings hold character yE.g., customer names, addresses, product names, descriptions. yYou enclose strings in quote marks (i.e., “ “ )  Consider the following example. y $name="Christopher"; y $preference="Milk Shake“; $name -> “Christopher $preference -> “Milk Shake”

22 David Lash 22 A String is not a number zYou cannot add, subtract, multiple, etc variables that hold strings yWhat would “Dave” / “Smith” be? zIf use string as number won’t get error yPHP will try to convert to a 0  For example, will output ”y=1” (not an error). <?php $x ="banana"; $sum = 1 + $x; print ("y=$sum"); ?>

23 David Lash 23 One thing you can do is.. zYou can concatenate strings together yThink of this as combining them. yFor example, combine 2 separate string variables into one.  For example, $fullname = $firstname. $lastname;  $fullname will receive the string values of $firstname and $lastname connected together.  For example, $firstname = "John"; $lastname = "Smith"; $fullname = $firstname. $lastname; print ("Fullname=$fullname"); Would output Fullname=JohnSmith The concatenate operator

24 David Lash 24 Another way to concatenate  You can also use double quotation marks to create  concatenation directly,  For example, x $Fullname2 = "$FirstName $LastName"; x This statement has the same effect as x $Fullname2 = $FirstName. " ". $LastName;

25 David Lash 25 The strlen() Function zMost string functions require you to send them one or more arguments. z 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 example

26 David Lash 26 The strlen() Function Example <?php $comments = "Good Job"; $len = strlen($comments); print ("Length=$len"); ?> This PHP script would output “ Length=8 ”. Note: Why would I do this? Suppose input comes in from text area, can tell how many characters they typed in.

27 David Lash 27 Objectives zTo learn how to store and access data in PHP variables zTo understand how to create and manipulate numeric and string variables zTo learn how to pass data from HTML forms to PHP scripts

28 David Lash 28 HTML Forms and not part of PHP language but important way to send data to scripts Recall HTML Input Forms Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button

29 David Lash 29 Starting And Ending HTML Forms You can create HTML forms by using the HTML and tags.

30 David Lash 30 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. This script contains the line: print “A simple initial script”;

31 David Lash 31 A Full Example... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/form1.html Question: What do you think you would get if First.php was not there?

32 David Lash 32 So now lets send data to a PHP Script  To receive HTML form input into a PHP script:  Use $_POST[ ] and “get” the form element’s name argument.  For example, if form uses the following:   Then form-handling PHP script could “get” the value of form variable contact using $my_var= $_POST[“contact”];  If user clicks radio button, then $my_var would = “Yes” contact is set here and received here

33 David Lash 33 Slow that down a little...  To receive data use a special variable called $_POST. y $my_var= $_POST[“name”]; Enclose in square bracket and then quotes Name of HTML form 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.

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

35 David Lash 35 A Full Example... The previous code can be executed at http://webwizard.aw.com/~phppgm/C2/Form4Radio_NG.html http://webwizard.aw.com/~phppgm/C2/Form4Radio_NG.html

36 David Lash 36 Sending email from PHP scripts zSometimes 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.

37 David Lash 37 Full Example, of sending email 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. ?>

38 David Lash 38 How send email again? Survey Form Car Survey Name: What kind of options do you want on your car? Electric windows AM/FM Radio Turbo Charger Automatic Transmission See http://condor.depaul.edu/~dlash/website/exampleform.htmlhttp://condor.depaul.edu/~dlash/website/exampleform.html

39 David Lash 39 Email PHP Script... <?php $fname = $_POST["fname"]; $extras = $_POST["extras"]; $dest='dlash@condor.depaul.edu'; $subject = "New Survey Data from $fname"; $message = "name=$fname \n extras=$extras"; $extra='From: harry@yahoo.com'; mail( $dest, $subject, $message, $extra ); print ' Email results '; print ' '; print "Just sent email to $dest with form data from $fname"; print " subject=$subject"; print " "; ?>

40 David Lash 40 Another Example Average … Survey Form Class Survey Pick A Number: Pick A Number 2: Pick A Number 3: http://condor.depaul.edu/~dlash/extra/Webpage/examples/average.html

41 David Lash 41 The PHP Code … Guess the Dice Your Averages Are: <?php $num1 = $_POST[num1]; $num2 = $_POST[num2]; $num3 = $_POST[num3]; $aver = ($num1 + $num2 + $num3 ) / 3; print " "; print "num1 = $num1 "; print "num2 = $num2 "; print "num3 = $num3 "; print " "; print " aver = $aver"; print " "; ?>

42 David Lash 42 Summary zTo learn how to store and access data in PHP variables zTo understand how to create and manipulate numeric and string variables y$x = $y + 2; y$apple = $x + 5; y$first = “George”; y$last = “Bush”; y$full = “$first W. $last”; zTo learn how to pass data from HTML forms to PHP scripts yReceive as follows: x$name=$_POST[‘name’];


Download ppt "David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 2 Instructor: David A. Lash Enhancing HTML With PHP."

Similar presentations


Ads by Google