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

Slides:



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

JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Computer Programming w/ Eng. Applications
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
The Web Warrior Guide to Web Design Technologies
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 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
1 Controlling Script Flow! David Lash Chapter 3 Conditional Statements.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
David Lash DePaul University SNL 262 Web Page Design Web Wizard’s Chapter 3 Instructor: David A. Lash Controlling the instruction flow.
1 Intro To PHP David Lash Getting Started With PHP.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 Wanda M. Kunkle.
Guide To UNIX Using Linux Third Edition
1 PHP and MySQL David Lash Module 2 Working with forms, PHP conditionals and loops.
Introduction to scripting
Web Programming Basics of PHP 1. Objectives To learn how to store and access data in PHP variables To understand how to create and manipulate numeric.
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.
Chapter 4 Handling User Input PHP Programming with MySQL 2nd Edition
PHP : Hypertext Preprocessor
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.
INTERNET APPLICATION DEVELOPMENT For More visit:
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)PHP Recap.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Nael Alian Introduction to PHP
A little PHP. Enter the simple HTML code seen below.
Using Client-Side Scripts to Enhance Web Applications 1.
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
4 1 Array and Hash Variables CGI/Perl Programming By Diane Zak.
DHTML AND JAVASCRIPT Genetic Computer School LESSON 5 INTRODUCTION JAVASCRIPT G H E F.
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.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction to Programming with RAPTOR
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Strings, output, quotes and comments
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Variables and Strings. Variables  When we are writing programs, we will frequently have to remember a value for later use  We will want to give this.
1 Web Wizards Guide To PHP David Lash Chapter 2 Using Variables.
PHP Form Processing * referenced from
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 7-1 The Web Wizard’s Guide to PHP by David Lash.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
A little PHP. Enter the simple HTML code seen below.
A little PHP.
Receiving form Variables
Chapter 6 JavaScript: Introduction to Scripting
Variables, Expressions, and IO
PHP Introduction.
Intro to PHP & Variables
HTML Forms and User Input
More Selections BIS1523 – Lecture 9.
Number and String Operations
WEB PROGRAMMING JavaScript.
PHP.
T. Jumana Abu Shmais – AOU - Riyadh
The Web Wizard’s Guide to PHP by David A. Lash
Presentation transcript:

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

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

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”; ?>

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

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.

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

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.

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

David Lash 9 A Full Example Variable Example <?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

David Lash 10 Another way to think about this Variable Example <?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

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

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

David Lash 13 Common PHP Numeric Operators

David Lash 14 A Full Example Variable Example <?php 5. $columns = 20; 6. $rows = 12; 7. $total_seats = $rows * $columns; $ticket_cost = 3.75; 10. $total_revenue = $total_seats * $ticket_cost; $building_cost = 300; 13. $profit = $total_revenue - $building_cost; print ("Total Seats are $total_seats "); 16. print ("Total Revenue is $total_revenue "); 17. print ("Total Profit is $profit"); 18. ?>

David Lash 15 A Full Example... See Variable Example <?php 5. $columns = 20; 6. $rows = 12; 7. $total_seats = $rows * $columns; $ticket_cost = 3.75; 10. $total_revenue = $total_seats * $ticket_cost; $building_cost = 300; 13. $profit = $total_revenue - $building_cost; print ("Total Seats are $total_seats "); 16. print ("Total Revenue is $total_revenue "); 17. print ("Total Profit is $profit"); 18. ?>

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

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 = * 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.

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 = * 2;  $y = (10 * 2);  $z = ( ) * 2;

David Lash 19 A Full Example Expression Example <?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).

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

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”

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

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

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;

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

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.

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

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

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

David Lash 30 Another Full Script Example A Simple Form 3. 4.<form action=" method="post" > 5. Click submit to start our initial PHP program This script contains the line: print “A simple initial script”;

David Lash 31 A Full Example... The previous code can be executed at Question: What do you think you would get if First.php was not there?

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

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.

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

David Lash 35 A Full Example... The previous code can be executed at

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

David Lash 37 Full Example, of sending 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"); 10. ?>

David Lash 38 How send 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

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

David Lash 40 Another Example Average … Survey Form Class Survey Pick A Number: Pick A Number 2: Pick A Number 3:

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

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’];