Chapter 1 Site management  & Sending & Receiving data between pages.
Dynamic web page decisions <ul><li>The first decision is to decide what scripting language you are going to write your dyn...
How PHP Pages are Accessed and Interpreted We will use WAMPS (Windows, apache, mysql, php server) and it will be locally i...
WAMPS <ul><li>This web service has three servers; </li></ul><ul><ul><li>Apache </li></ul></ul><ul><ul><li>MYSQL  </li></ul...
Creating a PHP Script File and Saving It to a Local Disk <ul><li>You can use a number of different editors to create your ...
Alternative PHP Delimiters <ul><li>You can alternatively start your PHP scripts with the <script> tag as follows: </li></u...
A Little About PHP's Syntax <ul><li>Some PHP Syntax Issues:   </li></ul><ul><ul><li>Be careful to use quotation marks, par...
Embedding PHP Statements Within HTML Documents <ul><li>One way to  use PHP is to embed PHP scripts within HTML tags in an ...
Using Backslash () to Generate HTML Tags with print() <ul><li>Sometimes you want to output an HTML tag that also requires ...
Using Comments with PHP cripts <ul><li>Comments enable you to include descriptive text along with the PHP script. </li></u...
Using Comments with  PHP Scripts <ul><li>Comment Syntax - Use // </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>//...
Example Script with Comments <ul><li>1. <html> <head> </li></ul><ul><li>2. <title> Generating HTML From PHP</title> </head...
Alternative Comment Syntax <ul><li>PHP allows a couple of additional ways to create comments. </li></ul><ul><ul><li><?php ...
Using PHP Variables <ul><li>Variables  are used to store and access data in computer memory.  </li></ul><ul><li>A  variabl...
Assigning New Values to  Variables <ul><li>You can assign new values to variables: </li></ul><ul><ul><ul><ul><li>$days = 3...
Selecting Variable Names <ul><li>You can select just about any set of characters for a variable name in PHP, but they must...
Combining Variables and the  print  Statement <ul><li>That is, to print out the value of $x, write the following PHP state...
A Full Example ... <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Variable Example </title> </head> </li></ul><ul><l...
Using Arithmetic Operators <ul><li>You can use  operators  such as a plus sign (+) for addition and a minus sign (–) for s...
Common PHP Numeric Operators
A Full Example <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Variable Example </title> </head> </li></ul><ul><li>3....
WARNING: Using Variables with Undefined Values If you accidentally use a variable that does not have a value assigned to i...
Writing Complex Expressions <ul><li>Operator precedence rules   define the order in which the operators are evaluated. For...
PHP Precedence Rules <ul><li>PHP follows the precedence rules listed below. </li></ul><ul><ul><li>First it evaluates opera...
PHP Precedence Rules <ul><li>For example, the first 2 statements evaluate to 80 while the last to 180.   </li></ul><ul><ul...
A Full Example <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Expression Example </title> </head> </li></ul><ul><li>...
Working with PHP String Variables <ul><li>Character strings are used in scripts to hold data such as customer names, addre...
WARNING: Be Careful Not to  Mix Variable Types <ul><li>Be careful not to mix string and numeric variable types.  </li></ul...
Using the Concatenate Operator <ul><li>The concatenate operator combines  two separate string variables into one.  </li></...
TIP;  An Easier Way to Concatenate Strings <ul><li>You can also use double quotation marks to create </li></ul><ul><li>con...
The strlen() Function <ul><li>Most string functions require you to send them one or more arguments. </li></ul><ul><li>Argu...
The strlen() Function Example <ul><li><?php  </li></ul><ul><li>$comments = &quot;Good Job&quot;; </li></ul><ul><li>$len = ...
The trim() Function <ul><li>This function removes any blank characters from the beginning and end of a  string. For exampl...
The strtolower() and  strtoupper() Functions <ul><li>These functions return the input string in all uppercase or all lower...
The substr() Function <ul><ul><li>Substr has the following general format: </li></ul></ul>
The substr() Function <ul><li>The substr() function enumerates character positions starting with 0 (not 1),  </li></ul><ul...
The substr() Function <ul><li>As another example, consider the following use of the substr() function </li></ul><ul><ul><l...
Servers and files <ul><li>The php files need to be located on the server. </li></ul><ul><li>The location for wamps is  </l...
Dynamic Web pages requirements <ul><li>Create a local site: shown on the next slide. </li></ul><ul><li>Check WAMPS is runn...
Creating a local site Local folder is your H or F drive etc.
Creating a remote site Note:  You will have to create this folders, newlandStart. This can be done using the browse button...
Creating a Testing Server
Transferring to the Remote server <ul><li>Click on the “expand show local & remote sites” button. </li></ul><ul><li>Click ...
Changing htm to php <ul><li>Change all the htm & html pages to php.  </li></ul><ul><li>This is done by click on the name i...
Check the pages <ul><li>Open each page one at a time followed by the “f12” key to execute the page in the browser. </li></...
Using forms to send data. <ul><li>Overview:  </li></ul><ul><ul><li>In this section you will create pages with embedded for...
Retrieving data <ul><li>Overview: </li></ul><ul><ul><li>The are four ways of transferring data from one page to another. <...
Creating HTML Input Forms <ul><li>HTML Forms are not part of PHP language but important way to send data to scripts  </li>...
Starting And Ending HTML Forms <ul><li>You can create HTML forms by using the HTML  <form>  and  </form>  tags.  </li></ul>
Creating Form Buttons <ul><li>You can create submit and reset buttons by placing the following within <form> & </form> tag...
Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 4.<form action=&quot;http://...
Receiving Form Input into PHP Scripts <ul><li>To receive HTML form input into a PHP script:  </li></ul><ul><ul><li>Use a P...
Full Example <ul><li>Suppose your HTML form uses the following:  </li></ul><ul><ul><li>Enter email address: <input type=&q...
Register_Globals? <ul><li>Since PHP 4.2.1, the default PHP configuration is requiring a different mechanism to receive inp...
How can you tell if Register_Globals is OFF? <ul><li>Enter the following PHP script and run it.  </li></ul><ul><ul><li><?P...
Getting input data with Register_Globals OFF? <ul><li>To receive data with REGISTER_GOBALS OFF you use a special variable ...
Full Example, when REGISTER_GLOBALS is OFF. <ul><li>Suppose your HTML form uses the following:  </li></ul><ul><ul><li>Ente...
Forms sending & retrieving 1 <ul><li>Create a new page and save as test_form.php. </li></ul><ul><li>Title : Using a form. ...
Forms sending & retrieving 2 <ul><li>Properties of objects  </li></ul><ul><li>Textfield 1: </li></ul><ul><ul><li>Name= fir...
Forms sending & retrieving 3 <ul><li>Create another page called test_form_processor.php. </li></ul><ul><li>Title: Displayi...
Forms sending & retrieving 4 <ul><li>Select the form variable. </li></ul><ul><li>Select the Request.Form type. </li></ul><...
Forms sending & retrieving 5 <ul><li>Place the cursor after the comer after the “Thank you ”. </li></ul><ul><li>Click on t...
Forms sending & retrieving 6 <ul><li>Your page should look like this. </li></ul><ul><li>Now execute the test_form.php page...
Looking at the code. <ul><li>Normally php code is embedded with </li></ul><ul><ul><li><?php  ?> </li></ul></ul><ul><li>Ech...
Retrieving data via URL 1 <ul><li>Open test_form.php and save as test_form_get.php. </li></ul><ul><li>Change the form meth...
Retrieving data via URL 2 Select the URL Variable and the screen below will appear. Put the name of the variable from the ...
Looking at the code <ul><li>This code is similar to the form request, except the form has change to GET. </li></ul>
Sending data with Hyperlinks. <ul><li>When you send data with forms and a method of GET, information is sent in the URL. <...
Sending data with Hyperlinks 2. <ul><li>1 st  page.  </li></ul><ul><li>Click on a link, eg cat and the page appears like. ...
Sending data with Hyperlinks 3 <ul><li>Create a page and save it as animal_question.php and create it to look like this. <...
Sending data with Hyperlinks 4 <ul><li>The links on the animal_question.php page should look like.. </li></ul><ul><li>Cat ...
Sending data with Hyperlinks 5 <ul><li>Another way to set the link with parameters is, click on the browse icon select the...
Sending data with Hyperlinks 6 <ul><li>The animal_home_page.php should look like. </li></ul><ul><li>Create the URL Variabl...
Cookies - 1 <ul><li>The form and querystring request are stateless variables. Once the server has passed them onto the cli...
Cookies - 2 <ul><li>Cookies and security </li></ul><ul><ul><li>Cookies can’t be used to infect computer with a virus. </li...
Cookies - 3 <ul><li>Open the test_form_processor_get.php file . </li></ul><ul><li>Create a new page test_form_processor_co...
Cookies - 4 <ul><li>Create the bindings and place them in the design view of the test_form_processor_cookies.php file. </l...
Lab 1 <ul><li>Objectives  </li></ul><ul><li>After completing this lab, you should be able to:  </li></ul><ul><ul><li>Creat...
Lab 1 cont-2 <ul><li>Copy the folder lab_start from the x drive to your working drive. </li></ul><ul><li>Create a new site...
unit2sendingpage.php When you click on the “Display Home “ Button the next page appears showing the  city that was typed i...
unit2formactionpage.php Notice, the word “Frankston” has appeared. This was a form variable from the previous page.
unit2urlactionpage.php?firstname=Larry Notice “Larry” was sent in the URL and it is displayed  on this page. Put your name...
Upcoming SlideShare
Loading in …5
×

Dynamic Web Pages Ch 1 V1.0

2,392 views

Published on

Published in: Technology, Design
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,392
On SlideShare
0
From Embeds
0
Number of Embeds
27
Actions
Shares
0
Downloads
50
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 31/08/09 Database php version 2.1
  • 31/08/09 Database php version 2.1
  • Dynamic Web Pages Ch 1 V1.0

    1. 1. Chapter 1 Site management & Sending & Receiving data between pages.
    2. 2. Dynamic web page decisions <ul><li>The first decision is to decide what scripting language you are going to write your dynamic pages in. </li></ul><ul><ul><li>PHP: Generally uses an Apache server with php. </li></ul></ul><ul><ul><li>ASP: Generally uses Microsoft Internet Information Services (IIS), which has an asp engine incorporated. </li></ul></ul>
    3. 3. How PHP Pages are Accessed and Interpreted We will use WAMPS (Windows, apache, mysql, php server) and it will be locally installed on the lab machines.
    4. 4. WAMPS <ul><li>This web service has three servers; </li></ul><ul><ul><li>Apache </li></ul></ul><ul><ul><li>MYSQL </li></ul></ul><ul><ul><li>PHP </li></ul></ul><ul><li>Wamps will be located locally on the lab machines. So the lab machines will be a local and server machine. </li></ul>
    5. 5. Creating a PHP Script File and Saving It to a Local Disk <ul><li>You can use a number of different editors to create your PHP script files. </li></ul><ul><ul><li>The PHP script starts with a <?php tag and ends with ?>. </li></ul></ul><ul><ul><li>Between these tags is a single PHP print statement. </li></ul></ul>
    6. 6. Alternative PHP Delimiters <ul><li>You can alternatively start your PHP scripts with the <script> tag as follows: </li></ul><ul><ul><ul><li><script language=&quot;PHP&quot;> </li></ul></ul></ul><ul><ul><ul><li>print (&quot;A simple initial script&quot;); </li></ul></ul></ul><ul><ul><ul><li></script> </li></ul></ul></ul><ul><li>If have short_open_tag enabled in its configuration file, you can use <? and ?>. </li></ul><ul><li>If asp_tags is enabled in the PHP configuration file, you can use <% and %> as delimiters. </li></ul>
    7. 7. A Little About PHP's Syntax <ul><li>Some PHP Syntax Issues: </li></ul><ul><ul><li>Be careful to use quotation marks, parentheses, and brackets in pairs. </li></ul></ul><ul><ul><li>Most PHP commands end with a semicolon (;). </li></ul></ul><ul><ul><li>Be careful of case. </li></ul></ul><ul><ul><li>PHP ignores blank spaces. </li></ul></ul>
    8. 8. Embedding PHP Statements Within HTML Documents <ul><li>One way to use PHP is to embed PHP scripts within HTML tags in an HTML document. </li></ul><ul><li>1. <html> </li></ul><ul><li>2. <head> </li></ul><ul><li>3. <title>HTML With PHP Embedded</title> </head> </li></ul><ul><li>4. <body> </li></ul><ul><li>5. <font size=5 color=”blue”>Welcome To My Page</font> </li></ul><ul><li>6. <?php </li></ul><ul><li>7. print (&quot;<br> Using PHP is not hard<br>&quot;); </li></ul><ul><li>8. ?> </li></ul><ul><li>9. and you can learn to use it quickly! </li></ul><ul><li>10. </body></html> </li></ul>
    9. 9. Using Backslash () to Generate HTML Tags with print() <ul><li>Sometimes you want to output an HTML tag that also requires double quotation marks. </li></ul><ul><ul><li>Use the backslash (“”) character to signal that the double quotation marks themselves should be output: print (&quot;<font color=&quot;blue&quot;>&quot;); </li></ul></ul><ul><ul><li>The above statement would output: </li></ul></ul><ul><ul><li><font color=&quot;blue&quot;> </li></ul></ul>
    10. 10. Using Comments with PHP cripts <ul><li>Comments enable you to include descriptive text along with the PHP script. </li></ul><ul><ul><li>Comment lines are ignored when the script runs; they do not slow down the run-time. </li></ul></ul><ul><ul><li>Comments have two common uses. </li></ul></ul><ul><ul><ul><li>Describe the overall script purpose. </li></ul></ul></ul><ul><ul><ul><li>Describe particularly tricky script lines. </li></ul></ul></ul>
    11. 11. Using Comments with PHP Scripts <ul><li>Comment Syntax - Use // </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>// This is a comment </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>Can place on Same line as a statement: </li></ul><ul><li><?php </li></ul><ul><li>print (&quot;A simple initial script&quot;); //Output a line </li></ul><ul><li>?> </li></ul>
    12. 12. Example Script with Comments <ul><li>1. <html> <head> </li></ul><ul><li>2. <title> Generating HTML From PHP</title> </head> </li></ul><ul><li>3. <body> <h1> Generating HTML From PHP</h1> </li></ul><ul><li>4. <?php </li></ul><ul><li>5. // </li></ul><ul><li>6. // Example script to output HTML tags </li></ul><ul><li>7. // </li></ul><ul><li>8. print (&quot;Using PHP has <i>some advantages:</i>&quot;); </li></ul><ul><li>9. print (&quot;<ul><li>Speed</li><li>Ease of use</li> </li></ul><ul><li><li>Functionality</li></ul>&quot;); //Output bullet list </li></ul><ul><li>10. print (&quot;</body></html>&quot;); </li></ul><ul><li>11. ?> </li></ul>
    13. 13. Alternative Comment Syntax <ul><li>PHP allows a couple of additional ways to create comments. </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>phpinfo(); # This is a built-in function </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>Multiple line comments. <?php </li></ul><ul><ul><li>/* </li></ul></ul><ul><ul><li>A script that gets information about the </li></ul></ul><ul><ul><li>PHP version being used. </li></ul></ul><ul><ul><li>*/ </li></ul></ul><ul><ul><li><? phpinfo(); ?> </li></ul></ul>
    14. 14. Using PHP Variables <ul><li>Variables are used to store and access data in computer memory. </li></ul><ul><li>A variable name is a label used within a script to refer to the data. </li></ul>
    15. 15. Assigning New Values to Variables <ul><li>You can assign new values to variables: </li></ul><ul><ul><ul><ul><li>$days = 3; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$newdays = 100; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$days = $newdays; </li></ul></ul></ul></ul><ul><li>At the end of these three lines, $days and $newdays both have values of 100. </li></ul>
    16. 16. Selecting Variable Names <ul><li>You can select just about any set of characters for a variable name in PHP, but they must: </li></ul><ul><ul><li>Use a dollar sign ($) as the first character </li></ul></ul><ul><ul><li>Use a letter or an underscore character (_) as the second character. </li></ul></ul><ul><li>Note:Try to select variable names that help describe their function. For example $counter is more descriptive than $c or $ctr. </li></ul>
    17. 17. Combining Variables and the print Statement <ul><li>That is, to print out the value of $x, write the following PHP statement: </li></ul><ul><ul><li>print (&quot;$x&quot;); </li></ul></ul><ul><li>The following code will output “Bryant is 6 years old”. </li></ul><ul><ul><li>$age=6; </li></ul></ul><ul><ul><li>print (&quot;Bryant is $age years old.&quot;); </li></ul></ul>
    18. 18. A Full Example ... <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Variable Example </title> </head> </li></ul><ul><li>3. <body> </li></ul><ul><li>4. <?php </li></ul><ul><li>5. $first_num = 12; </li></ul><ul><li>6. $second_num = 356; </li></ul><ul><li>7. $temp = $first_num; </li></ul><ul><li>8. $first_num = $second_num; </li></ul><ul><li>9. $second_num = $temp; </li></ul><ul><li>10. print (&quot;first_num= $first_num <br> </li></ul><ul><li>second_num=$second_num&quot;); </li></ul><ul><li>11. ?> </body> </html> </li></ul>
    19. 19. Using Arithmetic Operators <ul><li>You can use operators such as a plus sign (+) for addition and a minus sign (–) for subtraction to build mathematical expressions. </li></ul><ul><li>For example </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$apples = 12; </li></ul></ul></ul><ul><ul><ul><li>$oranges = 14; </li></ul></ul></ul><ul><ul><ul><li>$total_fruit = $apples + $oranges; </li></ul></ul></ul><ul><ul><ul><li>print (&quot;The total number of fruit is $total_fruit&quot;); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><li>These PHP statements would output “The total number of fruit is 26.” </li></ul>
    20. 20. Common PHP Numeric Operators
    21. 21. A Full Example <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Variable Example </title> </head> </li></ul><ul><li>3. <body> </li></ul><ul><li>4. <?php </li></ul><ul><li>5. $columns = 20; </li></ul><ul><li>6. $rows = 12; </li></ul><ul><li>7. $total_seats = $rows * $columns; </li></ul><ul><li>8. </li></ul><ul><li>9. $ticket_cost = 3.75; </li></ul><ul><li>10. $total_revenue = $total_seats * $ticket_cost; </li></ul><ul><li>11. </li></ul><ul><li>12. $building_cost = 300; </li></ul><ul><li>13. $profit = $total_revenue - $building_cost; </li></ul><ul><li>14. </li></ul><ul><li>15. print (&quot;Total Seats are $total_seats <br>&quot;); </li></ul><ul><li>16. print (&quot;Total Revenue is $total_revenue <br>&quot;); </li></ul><ul><li>17. print (&quot;Total Profit is $profit&quot;); </li></ul><ul><li>18. ?> </body> </html> </li></ul>
    22. 22. WARNING: Using Variables with Undefined Values If you accidentally use a variable that does not have a value assigned to it will have no value (called a null value).When a variable with a null value is used in an expression PHP, PHP may not generate an error and may complete the expression evaluation. For example, the following PHP script will output x= y=4. <?php $y = 3; $y=$y + $x + 1; // $x has a null value print (&quot;x=$x y=$y&quot;); ?>
    23. 23. Writing Complex Expressions <ul><li>Operator precedence rules define the order in which the operators are evaluated. For example, </li></ul><ul><ul><ul><li>$x = 5 + 2 * 6; </li></ul></ul></ul><ul><li>The value of $x is either 42 or 17 depending on order of evaluation. </li></ul><ul><li>Since multiplication evaluated before addition operations, this expression evaluates to 17. </li></ul>
    24. 24. PHP Precedence Rules <ul><li>PHP follows the precedence rules listed below. </li></ul><ul><ul><li>First it evaluates operators within parentheses. </li></ul></ul><ul><ul><li>Next it evaluates multiplication and division operators. </li></ul></ul><ul><ul><li>Finally it evaluates addition and subtraction operators. </li></ul></ul>
    25. 25. PHP Precedence Rules <ul><li>For example, the first 2 statements evaluate to 80 while the last to 180. </li></ul><ul><ul><li>$x = 100 - 10 * 2; </li></ul></ul><ul><ul><li>$y = 100 - (10 * 2); </li></ul></ul><ul><ul><li>$z = (100 - 10) * 2; </li></ul></ul>
    26. 26. A Full Example <ul><li>1. <html> </li></ul><ul><li>2. <head> <title>Expression Example </title> </head> </li></ul><ul><li>3. <body> </li></ul><ul><li>4. <?php </li></ul><ul><li>5. $grade1 = 50; </li></ul><ul><li>6. $grade2 = 100; </li></ul><ul><li>7. $grade3 = 75; </li></ul><ul><li>8. $average = ($grade1 + $grade2 + $grade3) / 3; </li></ul><ul><li>9. print (&quot;The average is $average&quot;); </li></ul><ul><li>10. ?> </body> </html> </li></ul>
    27. 27. Working with PHP String Variables <ul><li>Character strings are used in scripts to hold data such as customer names, addresses, product names, and descriptions. </li></ul><ul><li>Consider the following example. </li></ul><ul><ul><li>$name=&quot;Christopher&quot;; </li></ul></ul><ul><ul><li>$preference=&quot;Milk Shake&quot;; </li></ul></ul><ul><li>$name is assigned “Christopher” and the variable $preference is assigned “Milk Shake”. </li></ul>
    28. 28. WARNING: Be Careful Not to Mix Variable Types <ul><li>Be careful not to mix string and numeric variable types. </li></ul><ul><li>For example, you might expect the following statements to generate an error message, but they will not. Instead, they will output “ y=1 ”. </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$x =&quot;banana&quot;; </li></ul></ul><ul><ul><li>$sum = 1 + $x; </li></ul></ul><ul><ul><li>print (&quot;y=$sum&quot;); </li></ul></ul><ul><ul><li>?> </li></ul></ul>
    29. 29. Using the Concatenate Operator <ul><li>The concatenate operator combines two separate string variables into one. </li></ul><ul><li>For example, </li></ul><ul><ul><li>$fullname = $firstname . $lastname; </li></ul></ul><ul><li>$fullname will receive the string values of $firstname and $lastname connected together. </li></ul><ul><li>For example, </li></ul><ul><ul><ul><ul><li>$firstname = &quot;John&quot;; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$lastname = &quot;Smith&quot;; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$fullname = $firstname . $lastname; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>print (&quot;Fullname=$fullname&quot;); </li></ul></ul></ul></ul>
    30. 30. TIP; An Easier Way to Concatenate Strings <ul><li>You can also use double quotation marks to create </li></ul><ul><li>concatenation directly, </li></ul><ul><li>For example, </li></ul><ul><ul><ul><li>$Fullname2 = &quot;$FirstName $LastName&quot;; </li></ul></ul></ul><ul><ul><ul><li>This statement has the same effect as </li></ul></ul></ul><ul><ul><ul><li>$Fullname2 = $FirstName . &quot; &quot; . $LastName; </li></ul></ul></ul>
    31. 31. The strlen() Function <ul><li>Most string functions require you to send them one or more arguments. </li></ul><ul><li>Arguments are input values that functions use in the processing they do. </li></ul><ul><li>Often functions return a value to the script based on the input arguments. For example </li></ul>
    32. 32. The strlen() Function Example <ul><li><?php </li></ul><ul><li>$comments = &quot;Good Job&quot;; </li></ul><ul><li>$len = strlen($comments); </li></ul><ul><li>print (&quot;Length=$len&quot;); </li></ul><ul><li>?> </li></ul>This PHP script would output “Length=8”.
    33. 33. The trim() Function <ul><li>This function removes any blank characters from the beginning and end of a string. For example, consider the following script: </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$in_name = &quot; Joe Jackson &quot;; </li></ul></ul><ul><ul><li>$name = trim($in_name); </li></ul></ul><ul><ul><li>print (&quot;name=$name$name&quot;); </li></ul></ul><ul><ul><li>?> </li></ul></ul>
    34. 34. The strtolower() and strtoupper() Functions <ul><li>These functions return the input string in all uppercase or all lowercase letters, respectively. </li></ul><ul><li>For example, </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li>$inquote = &quot;Now Is The Time&quot;; </li></ul></ul><ul><ul><li>$lower = strtolower($inquote); </li></ul></ul><ul><ul><li>$upper = strtoupper($inquote); </li></ul></ul><ul><ul><li>print (&quot;upper=$upper lower=$lower&quot;); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>The above would output “upper=NOW IS THE TIME lower=now is the time”. </li></ul>
    35. 35. The substr() Function <ul><ul><li>Substr has the following general format: </li></ul></ul>
    36. 36. The substr() Function <ul><li>The substr() function enumerates character positions starting with 0 (not 1), </li></ul><ul><ul><li>For example, in the string “Homer”, the “H” would be position 0, the “o” would be position 1, the “m” position 2, and so on. </li></ul></ul><ul><li>For example, the following would output “Month=12 Day=25”. </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$date = &quot;12/25/2002&quot;; </li></ul></ul></ul><ul><ul><ul><li>$month = substr($date, 0, 2); </li></ul></ul></ul><ul><ul><ul><li>$day = substr($date, 3, 2); </li></ul></ul></ul><ul><ul><ul><li>print (&quot;Month=$month Day=$day&quot;); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul>
    37. 37. The substr() Function <ul><li>As another example, consider the following use of the substr() function </li></ul><ul><ul><li>It does not include the third argument (and thus returns a substring from the starting position to the end of the search string). </li></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$date = &quot;12/25/2002&quot;; </li></ul></ul></ul><ul><ul><ul><li>$year = substr($date, 6); </li></ul></ul></ul><ul><ul><ul><li>print (&quot;Year=$year&quot;); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><li>The above script segment would output “Year=2002”. </li></ul>
    38. 38. Servers and files <ul><li>The php files need to be located on the server. </li></ul><ul><li>The location for wamps is </li></ul><ul><li>C:wampswww </li></ul><ul><li>All files must be place here. </li></ul><ul><li>In dreamweaver you will work on files that are local on your storage space: f, h etc. </li></ul><ul><li>When these local files are save they will automatically be uploaded to the server. </li></ul>
    39. 39. Dynamic Web pages requirements <ul><li>Create a local site: shown on the next slide. </li></ul><ul><li>Check WAMPS is running. </li></ul><ul><ul><li>On the lab machines sometimes the IIS starts first and blocks the port. If this happens you must stop the IIS. </li></ul></ul><ul><li>Create a dynamic and testing site: shown on the following slides. </li></ul>
    40. 40. Creating a local site Local folder is your H or F drive etc.
    41. 41. Creating a remote site Note: You will have to create this folders, newlandStart. This can be done using the browse button. wampwww is the default directory.
    42. 42. Creating a Testing Server
    43. 43. Transferring to the Remote server <ul><li>Click on the “expand show local & remote sites” button. </li></ul><ul><li>Click on the “put files” button; the blue arrow. </li></ul>
    44. 44. Changing htm to php <ul><li>Change all the htm & html pages to php. </li></ul><ul><li>This is done by click on the name in the file panel and changing the extension. ( same as renaming in windows). </li></ul><ul><li>An update screen will appear. </li></ul><ul><li>Click ok. </li></ul>
    45. 45. Check the pages <ul><li>Open each page one at a time followed by the “f12” key to execute the page in the browser. </li></ul><ul><li>Once all pages have been executed you should be able to test the links on each page. </li></ul><ul><li>If the links are not working, check they are to the asp extension pages. </li></ul>
    46. 46. Using forms to send data. <ul><li>Overview: </li></ul><ul><ul><li>In this section you will create pages with embedded forms. </li></ul></ul><ul><ul><li>The forms will have objects, such as text fields, submit buttons, radio buttons etc. </li></ul></ul><ul><ul><li>The data in the form objects will be sent to the server and be displayed on another page. </li></ul></ul>
    47. 47. Retrieving data <ul><li>Overview: </li></ul><ul><ul><li>The are four ways of transferring data from one page to another. </li></ul></ul><ul><ul><ul><li>Through a form with the post method. </li></ul></ul></ul><ul><ul><ul><li>Through a form with the get method. </li></ul></ul></ul><ul><ul><ul><li>Through a URL link </li></ul></ul></ul><ul><ul><ul><li>Through a cookie. </li></ul></ul></ul>
    48. 48. Creating HTML Input Forms <ul><li>HTML Forms are not part of PHP language but important way to send data to scripts </li></ul>Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button
    49. 49. Starting And Ending HTML Forms <ul><li>You can create HTML forms by using the HTML <form> and </form> tags. </li></ul>
    50. 50. Creating Form Buttons <ul><li>You can create submit and reset buttons by placing the following within <form> & </form> tags. </li></ul><ul><li>The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase and Restart”. </li></ul>
    51. 51. Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 4.<form action=&quot;http://webwizard.aw.com/~phppgm/First.php&quot; method=&quot;post&quot; > 5. Click submit to start our initial PHP program. 6. <br> <input type=&quot;submit&quot; value=&quot;Click To Submit&quot;> 7. <input type=&quot;reset&quot; value=&quot;Erase and Restart&quot;> 8. </form> 9. </body> </html>
    52. 52. Receiving Form Input into PHP Scripts <ul><li>To receive HTML form input into a PHP script: </li></ul><ul><ul><li>Use a PHP var name that matches the variable defined in the form element’s name argument. </li></ul></ul><ul><li>For example, if form uses the following: </li></ul><ul><ul><li><input type= &quot; radio &quot; name= &quot; contact &quot; value= &quot; Yes &quot; > </li></ul></ul><ul><li>Then form-handling PHP script could use a variable called $contact. </li></ul><ul><ul><li>If the user clicks the radio button, then $contact would = Yes </li></ul></ul>
    53. 53. Full Example <ul><li>Suppose your HTML form uses the following: </li></ul><ul><ul><li>Enter email address: <input type=&quot;text&quot; size=&quot;16&quot; maxlength=&quot;20&quot; name=&quot;email&quot;> </li></ul></ul><ul><li>Then can receive input as follows: </li></ul><ul><li>1. <html> </li></ul><ul><li>2. <head><title> Receiving Input </title> </head> </li></ul><ul><li>3. <body> </li></ul><ul><li>4. <font size=5>Thank You: Got Your Input.</font> </li></ul><ul><li>5. <?php </li></ul><ul><li>6. print (&quot;<br>Your email address is $email&quot;); </li></ul><ul><li>7. </li></ul><ul><li>8. print (&quot;<br> Contact preference is $contact&quot;); </li></ul><ul><li>9. ?> </li></ul>
    54. 54. Register_Globals? <ul><li>Since PHP 4.2.1, the default PHP configuration is requiring a different mechanism to receive input for security reasons (than the one just shown). </li></ul><ul><ul><li>Technical details: it is a PHP configuration option to turn REGISTER_GLOBALS OFF (new default) or ON in the php.ini configuration file. </li></ul></ul><ul><li>If your site has REGISTER_GLOBALS OFF you must use a different mechanism to receive HTML Form Variables. </li></ul>
    55. 55. How can you tell if Register_Globals is OFF? <ul><li>Enter the following PHP script and run it. </li></ul><ul><ul><li><?PHP phpinfo(); ?> </li></ul></ul><ul><li>Search through the output for REGISTER_GLOBALS and see if it is set to OFF or ON. </li></ul><ul><li>If it is off you must use the following way to receive input data. </li></ul>
    56. 56. Getting input data with Register_Globals OFF? <ul><li>To receive data with REGISTER_GOBALS OFF you use a special variable called $POST. </li></ul><ul><ul><li>$name $_POST[“name”]; </li></ul></ul>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.
    57. 57. Full Example, when REGISTER_GLOBALS is OFF. <ul><li>Suppose your HTML form uses the following: </li></ul><ul><ul><li>Enter email address: <input type=&quot;text&quot; size=&quot;16&quot; maxlength=&quot;20&quot; name=&quot;email&quot;> </li></ul></ul><ul><li>Then can receive input as follows: </li></ul><ul><li>1. <html> </li></ul><ul><li>2. <head><title> Receiving Input </title> </head> </li></ul><ul><li>3. <body> </li></ul><ul><li>4. <font size=5>Thank You: Got Your Input.</font> </li></ul><ul><li>5. <?php </li></ul><ul><li>6. $email = $_POST[“email”]; </li></ul><ul><li>7. $contact = $_POST[“contact”]; </li></ul><ul><li>8. print (&quot;<br>Your email address is $email&quot;); </li></ul><ul><li>9. print (&quot;<br> Contact preference is $contact&quot;); </li></ul><ul><li>10. ?> </li></ul>
    58. 58. Forms sending & retrieving 1 <ul><li>Create a new page and save as test_form.php. </li></ul><ul><li>Title : Using a form. </li></ul><ul><li>Add a form, 2 textfield and a submit button. </li></ul><ul><li>The page should look like; </li></ul>
    59. 59. Forms sending & retrieving 2 <ul><li>Properties of objects </li></ul><ul><li>Textfield 1: </li></ul><ul><ul><li>Name= firstName </li></ul></ul><ul><li>Textfield 2: </li></ul><ul><ul><li>Name=lastName </li></ul></ul><ul><li>Form </li></ul><ul><ul><li>Name=frmName </li></ul></ul><ul><ul><li>Action=test_form_processor.php </li></ul></ul><ul><ul><li>method-=POST. </li></ul></ul>
    60. 60. Forms sending & retrieving 3 <ul><li>Create another page called test_form_processor.php. </li></ul><ul><li>Title: Displaying form data. </li></ul><ul><li>Create the page as shown and select the plus to bind the form data. </li></ul>
    61. 61. Forms sending & retrieving 4 <ul><li>Select the form variable. </li></ul><ul><li>Select the Request.Form type. </li></ul><ul><li>The name should be the same as the name on the page the data is coming from. </li></ul>In this case the data is coming from test_form.php
    62. 62. Forms sending & retrieving 5 <ul><li>Place the cursor after the comer after the “Thank you ”. </li></ul><ul><li>Click on the binding firstName and then click on the insert button. This inserts the request variable into that space on the page. </li></ul><ul><li>To insert the binding you can also drag and drop as another method. </li></ul><ul><li>Repeat this for the lastName variable. </li></ul>
    63. 63. Forms sending & retrieving 6 <ul><li>Your page should look like this. </li></ul><ul><li>Now execute the test_form.php page. </li></ul>Note: You will need to put a non breaking space (&nbsp;) between the two form bindings
    64. 64. Looking at the code. <ul><li>Normally php code is embedded with </li></ul><ul><ul><li><?php ?> </li></ul></ul><ul><li>Echo is the command to display information. There are two other commands: print() and printf(). </li></ul><ul><li>The two print command allow formatting of text. </li></ul>
    65. 65. Retrieving data via URL 1 <ul><li>Open test_form.php and save as test_form_get.php. </li></ul><ul><li>Change the form method to GET. </li></ul><ul><li>Open test_form_processor.php file and save as test_form_processor_get.php. </li></ul><ul><li>Delete the bindings. </li></ul><ul><li>Insert 2 new bindings: </li></ul><ul><ul><li>the URLVariable. </li></ul></ul><ul><ul><li>Next slide shows how. </li></ul></ul>
    66. 66. Retrieving data via URL 2 Select the URL Variable and the screen below will appear. Put the name of the variable from the page calling this one, as shown; firstName The new binding as shown will appear. Drag it to where the variable should be shown. The next slide shows this.
    67. 67. Looking at the code <ul><li>This code is similar to the form request, except the form has change to GET. </li></ul>
    68. 68. Sending data with Hyperlinks. <ul><li>When you send data with forms and a method of GET, information is sent in the URL. </li></ul><ul><li>Another method of sending the info in the URL is in a Hyperlink. </li></ul><ul><li>To do this you can just add the information in a name and info pair to the url of the link. eg: </li></ul><ul><ul><li>?firstName=Larry </li></ul></ul>
    69. 69. Sending data with Hyperlinks 2. <ul><li>1 st page. </li></ul><ul><li>Click on a link, eg cat and the page appears like. </li></ul><ul><li>Click on dog and the page appears like. </li></ul>
    70. 70. Sending data with Hyperlinks 3 <ul><li>Create a page and save it as animal_question.php and create it to look like this. </li></ul><ul><li>The links are to the page animal_home_page.php </li></ul>
    71. 71. Sending data with Hyperlinks 4 <ul><li>The links on the animal_question.php page should look like.. </li></ul><ul><li>Cat </li></ul><ul><li>Dog </li></ul>
    72. 72. Sending data with Hyperlinks 5 <ul><li>Another way to set the link with parameters is, click on the browse icon select the file and click on the parameters buttons as shown. </li></ul>
    73. 73. Sending data with Hyperlinks 6 <ul><li>The animal_home_page.php should look like. </li></ul><ul><li>Create the URL Variable as before with the binding panels. </li></ul>
    74. 74. Cookies - 1 <ul><li>The form and querystring request are stateless variables. Once the server has passed them onto the client the server has no memory of them. </li></ul><ul><li>Cookies are tiny text files that are written onto the user’s hard drive. </li></ul><ul><li>Cookies help to maintain state over coming the stateless HTTP protocol. </li></ul>
    75. 75. Cookies - 2 <ul><li>Cookies and security </li></ul><ul><ul><li>Cookies can’t be used to infect computer with a virus. </li></ul></ul><ul><ul><li>Cookies can only be read by the site that sent them. </li></ul></ul><ul><ul><li>Cookies should never contain credit card details, password, etc. </li></ul></ul>
    76. 76. Cookies - 3 <ul><li>Open the test_form_processor_get.php file . </li></ul><ul><li>Create a new page test_form_processor_cookies.php </li></ul><ul><li>Add the following to the test_form_processor_get.php file. </li></ul>This code must be place before the html tag Cookie variable name
    77. 77. Cookies - 4 <ul><li>Create the bindings and place them in the design view of the test_form_processor_cookies.php file. </li></ul><ul><li>As shown, then save and preview. </li></ul>
    78. 78. Lab 1 <ul><li>Objectives </li></ul><ul><li>After completing this lab, you should be able to: </li></ul><ul><ul><li>Create URL variables in the Bindings panel. </li></ul></ul><ul><ul><li>Display URL variables on a page. </li></ul></ul><ul><ul><li>Create FORM variables in the Bindings panel. </li></ul></ul><ul><ul><li>Display FORM variables on a page. </li></ul></ul>
    79. 79. Lab 1 cont-2 <ul><li>Copy the folder lab_start from the x drive to your working drive. </li></ul><ul><li>Create a new site called lab </li></ul><ul><li>Setup the folders on the IIS root folder and the remote and testing servers. </li></ul>
    80. 80. unit2sendingpage.php When you click on the “Display Home “ Button the next page appears showing the city that was typed in on this page. If you click on the link “Click Here” then it takes you to the “unit2urlactionpage.php”. The link passes your name in the URL.
    81. 81. unit2formactionpage.php Notice, the word “Frankston” has appeared. This was a form variable from the previous page.
    82. 82. unit2urlactionpage.php?firstname=Larry Notice “Larry” was sent in the URL and it is displayed on this page. Put your name there instead.

    ×