SlideShare a Scribd company logo
28/06/2009




                                       Dynamic Web Pages
                                                                        PHP




                                                                                                            1




                                Authors & Modifications


                 First Edition 2006   Version   1.0            1.1            2.0            2.1
                                      No.

                 Authors              Date      07/06          08/06          07/08          07/09


                 Cathie Usher         Contact   Cathie Usher   Cathie Usher   Cathie Usher   Cathie Usher




                                                                                                                2




Database php version 2.1                                                                                                    1
28/06/2009




                                         Chapter 1
                                           Site management
                                                   &
                                Sending & Receiving data between pages.




                                                                          3




                       Dynamic web page decisions
                     • The first decision is to decide what
                       scripting language you are going to
                           p g      g g y         g g
                       write your dynamic pages in.
                           – PHP: Generally uses an Apache server
                             with php.
                           – ASP: Generally uses Microsoft Internet
                             Information Ser ces ( S), wh ch has
                              nformat on Services (IIS), which
                             an asp engine incorporated.



                                                                              4




Database php version 2.1                                                                  2
28/06/2009




                          How PHP Pages are
                       Accessed and Interpreted
                               Your PC                                                               WebServer
                            (Internet connected)                                                   (Internet connected)

                             1. Web Browser        2. Se                                     Web Server
                                                        nd Re
                                                             ques
                             Please Enter
                                                                  t for    PHP                Software
                                                                               file
                             A
                             Phone
                                                                                         3. Receive
                             Number
                                                                                        request, find
                                                                                      file and read it.
                            Submit Erase
                                                                                                          4. Execute
                                                                                                             PHP
                                                                                                          statements


                             7. Web Browser                                                5. Send
                              Web Browser                                                  results
                             Phone Query                                                    back.
                                                                                            back
                                                                           lts
                             Results:
                                                                  nR   esu
                                                             etur
                             That is                    6. R
                             John Doe's
                             Phone
                             Number                                               We will use WAMPS
                                                                                  (Windows, apache, mysql, php
                                                                                  server) and it will be locally
                                                                                  installed on the lab machines.          5




                                                        WAMPS
                     • This web service has three servers;
                           – Apache
                              p
                           – MYSQL
                           – PHP
                     • Wamps will be located locally on the
                       lab machines. So the lab machines will
                       be local and server machine.
                       b al    l ds             hi



                                                                                                                          6




Database php version 2.1                                                                                                              3
28/06/2009




                  Creating a PHP Script File and
                    Saving It to a Local Disk
                You can use a number of different editors to
                 create your PHP script files.
                              P P       fl
                    – The PHP script starts with a <?php tag and ends with ?>.
                    – Between these tags is a single PHP print statement.




                                                                           7




                    Alternative PHP Delimiters
                   • You can alternatively start your PHP
                     scripts with the <script> tag as follows:
                         p                 p     g    f
                           <script language="PHP">
                           print ("A simple initial script");
                           </script>

                   • If have short_open_tag enabled in its
                     configuration file, you can use <? and ?>.
                   • If asp_tags is enabled in the PHP
                     configuration file, you can use <% and %>
                     as delimiters.
                                                                           8




Database php version 2.1                                                                 4
28/06/2009




                  A Little About PHP's Syntax

                  • Some PHP Syntax Issues:
                     – Be careful to use quotation marks, parentheses, and
                       brackets in pairs.
                     – Most PHP commands end with a semicolon (;).
                     – Be careful of case.
                     – PHP ignores blank spaces.
                                    l




                                                                         9




                     Embedding PHP Statements Within
                            HTML Documents
                  • One way to use PHP is to embed PHP scripts
                    within HTML tags in an HTML document.
                  1. <html>
                  2. <head>
                  3. <title>HTML With PHP Embedded</title> </head>
                  4. <body>
                  5. <font size=5 color=”blue”>Welcome To My Page</font>
                  6. <?php
                  7.     print ("<br> Using PHP is not hard<br>");
                  8.
                  8 ?>
                  9. and you can learn to use it quickly!
                  10. </body></html>



                                                                         10




Database php version 2.1                                                              5
28/06/2009




                    Using Backslash () to Generate
                        HTML Tags with print()
                  • Sometimes you want to output an HTML
                    tag that also requires double quotation
                         h    l            d bl
                    marks.
                      – Use the backslash (“”) character to signal that the
                        double quotation marks themselves should be
                        output:
                        print ("<font color="blue">");
                      – The above statement would output:
                       <font color="blue">

                                                                           11




                  Using Comments with PHP cripts

                   • Comments enable you to include
                                       y
                     descriptive text along with the PHP
                     script.
                      – Comment lines are ignored when the
                        script runs; they do not slow down the
                        run-time.
                      – Comments have two common uses.
                           • Describe the overall script purpose.
                           • Describe particularly tricky script lines.

                                                                          12




Database php version 2.1                                                                6
28/06/2009




                           Using Comments with
                               PHP Scripts
                   • Comment Syntax - Use //
                              y
                      <?php
                      // This is a comment
                      ?>

                   • Can place on Same line as a statement:
                     <?php
                     print ("A simple initial script"); //Output a
                           ( A                script );
                     line
                     ?>



                                                                         13




                   Example Script with Comments
                  1. <html> <head>
                  2. <title> Generating HTML From PHP</title> </head>
                                      g               /         /
                  3. <body> <h1> Generating HTML From PHP</h1>
                  4. <?php
                  5. //
                  6. // Example script to output HTML tags
                  7. //
                  8. print ("Using PHP has <i>some advantages:</i>");
                  9. print ("<ul><li>Speed</li><li>Ease of use</li>
                  <li>Functionality</li></ul>"); //O
                   li      i   li   /li / l ") //Output b ll
                                                           bullet li
                                                                  list
                  10. print ("</body></html>");
                  11. ?>



                                                                         14




Database php version 2.1                                                              7
28/06/2009




                     Alternative Comment Syntax
                 PHP allows a couple of additional ways to
                  create comments.
                     <?php
                     phpinfo(); # This is a built-in function
                     ?>

                 • Multiple line comments. <?php
                     /*
                     A script that gets information about the
                     PHP version being used.
                     */
                     <? phpinfo(); ?>


                                                                                     15




                             Using PHP Variables
                     • Variables are used to store and
                       access data in computer memory.
                                           p           y
                     • A variable name is a label used
                       within a script to refer to the data.



                           Name of variable   $cost = 4.25;
                                                               Variables new value
                                               $months = 12;




                                                                                     16




Database php version 2.1                                                                          8
28/06/2009




                      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.



                                                                   17




                     Selecting Variable Names
                   • You can select just about any set of
                     characters for a variable name in PHP
                                                       PHP,
                     but they must:
                      – Use a dollar sign ($) as the first character
                      – Use a letter or an underscore character (_)
                        as the second character.
                   • N
                     Note:Try to select variable names that
                           T        l       i bl        h
                     help describe their function. For
                     example $counter is more descriptive
                     than $c or $ctr.                              18




Database php version 2.1                                                        9
28/06/2009




                       Combining Variables and the
                           print Statement

                   • That is, to print out the value of $x
                          is                            $x,
                     write the following PHP statement:
                      – print ("$x");

                   • The following code will output “Bryant is
                     6 years old”.
                      $age=6;
                      print ("Bryant is $age years old.");




                                                                      19




                             A Full Example ...
                  1. <html>
                  2.
                  2 <head> <title>Variable Example </title> </head>
                  3. <body>
                  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 <br>
                  second_num=$second_num");
                  11. ?> </body> </html>



                                                                      20




Database php version 2.1                                                          10
28/06/2009




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




                 Common PHP Numeric Operators

                    Table 2.1 Common PHP Numeric Operators

                    Operator   Effect           Example        Result

                    +          Addition         $x = 2 + 2;    $x is assigned 4.

                    -          Subtraction      $y = 3;        $y is assigned 2.
                                                $y = $y – 1;
                    /          Division         $y = 14 / 2;   $y is assigned 7.

                    *          Multiplication   $z = 4;        $y is assigned 16.
                                                $y = $z * 4;
                    %          Remainder        $y = 14 % 3;   $y is assigned 2.




                                                                                    22




Database php version 2.1                                                                        11
28/06/2009




                                A Full Example
                  1. <html>
                  2. <head> <title>Variable Example </title> </head>
                  3. <body>
                         y
                  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 <br>");
                  16. print ("Total Revenue is $total_revenue <br>");
                  17. print ("Total Profit is $profit");
                  18. ?> </body> </html>

                                                                        23




                   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 ("x=$x y=$y");
                 ?>
                                                                        24




Database php version 2.1                                                            12
28/06/2009




                  Writing Complex Expressions
                    • Operator precedence rules define
                      the d in hi h th
                      th order i which the operators are
                                                t
                      evaluated. For example,
                            $x = 5 + 2 * 6;
                    • The value of $x is either 42 or 17
                      depending on order of evaluation.
                        p      g            f
                    • Since multiplication evaluated before
                      addition operations, this expression
                      evaluates to 17.
                                                                25




                           PHP Precedence Rules

                    • PHP follows the precedence rules
                      listed below.
                       – First it evaluates operators within
                         parentheses.
                       – Next it evaluates multiplication and
                         division
                         di i i operators.
                                        t
                       – Finally it evaluates addition and
                         subtraction operators.

                                                                26




Database php version 2.1                                                    13
28/06/2009




                           PHP Precedence Rules
                     • For example, the first 2
                       statements evaluate to 80 while
                       the last to 180.
                           – $x = 100 - 10 * 2;
                           – $y = 100 - (10 * 2);
                           – $z = (100 - 10) * 2;




                                                             27




                               A Full Example
                  1. <html>
                  2.
                  2 <head> <title>Expression Example </title>
                    </head>
                  3. <body>
                  4. <?php
                  5. $grade1 = 50;
                  6. $grade2 = 100;
                  7. $grade3 = 75;
                  8. $average = ($grade1 + $grade2 + $grade3) /
                    3;
                  9. print ("The average is $average");
                  10. ?> </body> </html>
                                                             28




Database php version 2.1                                                 14
28/06/2009




                            Working with PHP String
                                  Variables
                  • Character strings are used in scripts to
                    hold data such as customer names
                                               names,
                    addresses, product names, and
                    descriptions.
                  • Consider the following example.
                         – $name="Christopher";
                         – $preference="Milk Shake";
                  • $name is assigned “Christopher” and the
                    variable $preference is assigned “Milk
                    Shake”.                                29




                     WARNING: Be Careful Not to
                        Mix Variable Types
                 • Be careful not to mix string and numeric variable types.
                 • For example, you might expect the following statements
                   to generate an error message, but they will not. Instead,
                   they will output “y=1”.
                    <?php

                    $x ="banana";

                    $sum = 1 + $x;

                    print ("y=$sum");

                    ?>




                                                                          30




Database php version 2.1                                                              15
28/06/2009




                           Using the Concatenate
                                 Operator
                  • The concatenate operator combines two
                    separate string variables i t one.
                          t t i        i bl into
                  • For example,
                      – $fullname = $firstname . $lastname;

                  • $fullname will receive the string values of
                    $firstname and $lastname connected together.
                                                         g
                  • For example,
                              $firstname = "John";
                              $lastname = "Smith";
                              $fullname = $firstname . $lastname;
                              print ("Fullname=$fullname");
                                                                          31




                       TIP; An Easier Way to
                        Concatenate Strings
                  • You can also use double quotation marks to
                                            q
                    create
                  • concatenation directly,
                  • For example,
                           • $Fullname2 = "$FirstName $LastName";
                           • Thi statement h
                             This t t     t has th same effect as
                                                the       ff t
                           • $Fullname2 = $FirstName . " " . $LastName;




                                                                          32




Database php version 2.1                                                              16
28/06/2009




                           The strlen() Function
                  • Most string functions require you to send
                    them one or more arguments.
                    th                             t
                  • 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
                                                  arguments.
                    example $len = strlen($name);
                    Receives the number of             Variable or value to work with
                     characters in $name          Name of function


                                                                                 33




                 The strlen() Function Example
                     <?php
                       p p
                                   $comments = "Good Job";
                                   $len = strlen($comments);
                                   print ("Length=$len");
                      ?>
                   This PHP script would output “Length=8”.
                                p           p       g




                                                                                 34




Database php version 2.1                                                                       17
28/06/2009




                             The trim() Function
                • This function removes any blank characters
                                           y
                  from the beginning and end of a string. For
                  example, consider the following script:
                    – <?php
                    –   $in_name = " Joe Jackson ";
                    –   $name = trim($in_name);
                    –   print ("name=$name$name");
                    –   ?>




                                                                35




                           The strtolower() and
                          strtoupper() Functions
                • These functions return the input string in all
                  uppercase or all l
                   pp    s      ll lowercase l tt s respectively.
                                          s letters, sp ti l
                • For example,
                    <?php
                    $inquote = "Now Is The Time";
                    $lower = strtolower($inquote);
                    $upper = strtoupper($inquote);
                    print ("upper=$upper lower=$lower");
                    ?>
                • The above would output “upper=NOW IS THE
                  TIME lower=now is the time”.

                                                                36




Database php version 2.1                                                    18
28/06/2009




                           The substr() Function
                    – Substr has the following general format:

                Assign the                                      Starting position to
                extracted sub-                                  start extraction from.
                string into this
                                 $part = substr( $name, 0, 5);
                variable.                                      Number of characters
                                                               to extract. (If omitted it will
                                    Extract from this          continue to extract until the end
                                    string variable
                                           variable.           of the string.)
                                                                f th t i )




                                                                                          37




                           The substr() Function
                • The substr() function enumerates character
                  positions starting with 0 (not 1)
                                                 1),
                    – 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.
                • For example, the following would output “Month=12
                  Day=25”.
                        <?php
                        $date = "12/25/2002";
                        $month = substr($date, 0, 2);
                        $day = substr($date, 3, 2);
                        print ("Month=$month Day=$day");
                        ?>                                                                38




Database php version 2.1                                                                                  19
28/06/2009




                            The substr() Function
                • As another example, consider the following
                  use of the substr() function
                    – It does not include the third argument (and thus
                      returns a substring from the starting position to
                      the end of the search string).
                       <?php
                       $date = "12/25/2002";
                       $year = substr($date, 6);
                       print ("Year=$year");
                       ?>

                • The above script segment would output
                  “Year=2002”.
                                                                   39




                               Servers and files
                     • The php files need to be located on
                       the server.
                     • The location for wamps is
                     C:wampswww
                     • All files must be place here.
                     • In dreamweaver you will work on files
                       that are local on your storage space:
                       f, h etc.
                     • When these local files are save they
                       will automatically be uploaded to the
                       server.                               40




Database php version 2.1                                                         20
28/06/2009




                   Dynamic Web pages requirements
                     • Create a local site: shown on the next
                       slide.
                     • Check WAMPS is running.
                           – On the lab machines sometimes the IIS
                             starts first and blocks the port. If this
                             happens you must stop the IIS.
                     • Create a dynamic and testing site:
                       shown on the following slides.


                                                                             41




                              Creating a local site

                                                          Local folder is your
                                                                          y
                                                          H or F drive etc.




                                                                             42




Database php version 2.1                                                                 21
28/06/2009




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




                           Creating a Testing Server




                                                                                44




Database php version 2.1                                                                    22
28/06/2009




                   Transferring to the Remote server
                     • Click on the “expand
                       show local & remote
                       sites” button.
                     • Click on the “put files”
                       button; the blue arrow.




                                                       45




                             Changing htm to php
                     • Change all the htm &
                       html pages to php.
                     • This is done by click on
                       the name in the file
                       panel and changing the
                       extension. (same as renaming
                       in windows).
                     • An update screen will
                       appear.
                       appear
                     • Click ok.


                                                       46




Database php version 2.1                                           23
28/06/2009




                                 Check the pages
                     • Open each page one at a time
                       followed by the “f12” key to execute
                                  y            y
                       the page in the browser.
                     • Once all pages have been executed
                       you should be able to test the links
                       on each page.
                     • If th li k are not working, check
                          the links      t     ki    h k
                       they are to the asp extension pages.

                                                                      47




                           Using forms to send data.
                     • Overview:
                           – In this section you will create pages
                             with embedded forms.
                              ith    b dd d f
                           – The forms will have objects, such as
                             text fields, submit buttons, radio
                             buttons etc.
                           – The data in the form objects will be
                             sent to the server and be displayed on
                             another page.



                                                                      48




Database php version 2.1                                                          24
28/06/2009




                                    Retrieving data
                     • Overview:
                           – The are four ways of transferring data
                                            y                g
                             from one page to another.
                             •   Through a form with the post method.
                             •   Through a form with the get method.
                             •   Through a URL link
                             •   Through a cookie.




                                                                              49




                      Creating HTML Input Forms
                • HTML Forms are not part of PHP language
                  but important way to send data to scripts


                                                              Text Box
                                                              Radio Buttons

                                                              Check Box
                                                               Select Box

                                                               Text Area

                                                              Submit/Reset button
                                                                              50




Database php version 2.1                                                                   25
28/06/2009




                   Starting And Ending HTML Forms

                • You can create HTML forms by using the
                  HTML <form> and </form> tags
                                            tags.
                                     Program to start when form is submitted.

                         <form action="http://webwizard.aw.com/~phppgm/program.php"
                               method="post">                                    Format to
                                                                                 send data.
                                             .
                                             .                  Place form elements between
                                                                <form> and </form> tags.


                         </form>
                                             .            Forms end with </form>




                                                                                                  51




                              Creating Form Buttons
                • You can create submit and reset buttons by
                  placing the following within <form> & </form>
                  tags.

                          <input type=”submit” value=”Click To Submit”>
                          <input type=”reset” value=”Erase and Restart”>
                      Type of
                  button to create                                                 Button Label


                • The submit button will be labeled “Click To
                  Submit”. The reset button will be labeled
                  “Erase and Restart”.
                                                                                                  52




Database php version 2.1                                                                                      26
28/06/2009




                      Another Full Script Example
                1.<html>
                2.<head> <title> A Simple Form </title> </head>
                3.<body>
                3 <body>
                4.<form
                  action="http://webwizard.aw.com/~phppgm/First.p
                  hp"
                         method="post" >
                5. Click submit to start our initial PHP program.
                6. <br> <input type="submit" value="Click To
                           p    yp
                  Submit">
                7. <input type="reset" value="Erase and Restart">
                8. </form>
                9. </body> </html>
                                                                                  53




                   Receiving Form Input into PHP
                              Scripts
                • To receive HTML form input into a PHP script:
                    – Use a PHP var name that matches the variable defined in the form
                      element’s name argument.

                • For example, if form uses the following:
                    – <input type="radio" name="contact" value="Yes">

                • Then form-handling PHP script could use a variable called
                  $
                  $contact.
                    – If the user clicks the radio button, then $contact would = Yes




                                                                                  54




Database php version 2.1                                                                        27
28/06/2009




                                  Full Example
                • Suppose your HTML form uses the following:
                    – Enter email address: <input type="text" size="16"
                      maxlength="20" name="email">
                            g

                • Then can receive input as follows:
                1.<html>
                2. <head><title> Receiving Input </title> </head>
                3. <body>
                4. <font size=5>Thank You: Got Your Input.</font>
                5.
                5 <?php
                6.    print ("<br>Your email address is $email");
                7.
                8.    print ("<br> Contact preference is $contact");
                9. ?>
                                                                          55




                             Register_Globals?
                • Since PHP 4.2.1, the default PHP
                  configuration is requiring a different
                  mechanism t receive i
                      h i    to      i input f security
                                             t for      it
                  reasons (than the one just shown).
                    – Technical details: it is a PHP configuration option
                      to turn REGISTER_GLOBALS OFF (new default)
                      or ON in the php.ini configuration file.
                • If your site has REGISTER_GLOBALS OFF
                  you must use a different mechanism to
                  receive HTML Form Variables.
                                                                          56




Database php version 2.1                                                              28
28/06/2009




                        How can you tell if
                     Register_Globals is OFF?
                • Enter the following PHP script and run it.
                                    g         p
                    – <?PHP phpinfo(); ?>
                • Search through the output for
                  REGISTER_GLOBALS and see if it is set to
                  OFF or ON.
                • If it i off you must use th f ll i way t
                        is ff         t    the following to
                  receive input data.

                                                                           57




                       Getting input data with
                       Register_Globals OFF?
                • To receive data with REGISTER_GOBALS
                  OFF you use a special variable called $POST.
                   FF                 l      bl    ll d $P
                                                      Enclose in square
                    – $name $_POST[“name”];           bracket and then quotes

                                                  Name of HTML form
                                                  variable (note do not use $)
                                                     i bl ( t d       t

                                   Special PHP Global variable. Technically it
                                   is an associative array (covered in chptr 5.)

                           PHP variable name that you want to receive      58
                           the HTML form input.




Database php version 2.1                                                                  29
28/06/2009




                     Full Example, when
                 REGISTER_GLOBALS is 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>
                        y
                4. <font size=5>Thank You: Got Your Input.</font>
                5. <?php
                6. $email = $_POST[“email”];
                7. $contact = $_POST[“contact”];
                8.     print ("<br>Your email address is $email");
                9.   print ("<br> Contact preference is $contact");
                                                                        59
                10. ?>




                    Forms sending & retrieving 1
                    • Create a new page and save as
                      test_form.php.
                    • Title : Using a form.
                    • Add a form, 2 textfield and a submit
                      button.
                    • The page should look like;




                                                                        60




Database php version 2.1                                                            30
28/06/2009




                  Forms sending & retrieving 2
                     • Properties of objects
                     • Textfield 1:
                           – N
                             Name= firstName
                                   fi tN
                     • Textfield 2:
                           – Name=lastName
                     • Form
                           – Name=frmName
                           – Action=test form processor php
                             Action=test_form_processor.php
                           – method-=POST.



                                                              61




                    Forms sending & retrieving 3
                     • Create another page called
                                    p         p p
                       test_form_processor.php.
                     • Title: Displaying form data.
                     • Create the page as shown and select
                       the plus to bind the form data.




                                                              62




Database php version 2.1                                                  31
28/06/2009




                  Forms sending & retrieving 4
                   • Select the form variable.
                   • Select the Request.Form
                     type.
                   • The name should be the
                     same as the name on the
                     page the data is coming
                     from.
                     f

                            In this case the data
                            is coming from
                            test_form.php                             63




                   Forms sending & retrieving 5
                     • Place the cursor after the comer after
                       the “Thank you ”.
                     • Click on the binding firstName and then
                       click on the insert button. This inserts the
                       request variable into that space on the
                       page.
                     • To insert the binding you can also drag and
                       drop as another method.
                       d           th      th d
                     • Repeat this for the lastName variable.


                                                                      64




Database php version 2.1                                                          32
28/06/2009




                   Forms sending & retrieving 6
                     • Your page should look like this.




                     • Now execute the test form php
                                       test_form.php
                       page.
                                   Note: You will need to put a non
                                   breaking space (&nbsp;) between the
                                   two form bindings
                                                                         65




                              Looking at the code.
                     •     Normally php code is embedded with
                           <?php   ?>




                     •     Echo is the command to display
                           information. There are two other
                           commands: print() and printf().
                     •     The two print command allow formatting
                           of text.
                                                                         66




Database php version 2.1                                                             33
28/06/2009




                           Retrieving data via URL 1
                     • Open test_form.php and save as
                       test_form_get.php.
                     • Change the form method to GET
                                                 GET.
                     • Open test_form_processor.php file and
                       save as test_form_processor_get.php.
                     • Delete the bindings.
                     • Insert 2 new bindings:
                           – the URLVariable
                                 URLVariable.
                           – Next slide shows how.



                                                                                    67




                       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
                                                                g
                                                will appear. Drag it to where
                                                the variable should be
                                                shown. The next slide shows
                                                this.


                                                                                    68




Database php version 2.1                                                                          34
28/06/2009




                              Looking at the code
                     • This code is similar to the form
                       request, except the form has change
                         q           p                  g
                       to GET.




                                                                    69




                    Sending data with Hyperlinks.
                     • When you send data with forms and a
                       method of GET, information is sent in the
                       URL.
                       URL
                     • Another method of sending the info in the
                       URL is in a Hyperlink.
                     • To do this you can just add the
                       information in a name and info pair to the
                       url of the link. eg:
                         l f th li k
                           – ?firstName=Larry


                                                                    70




Database php version 2.1                                                        35
28/06/2009




                   Sending data with Hyperlinks 2.
                     • 1st page.



                     • Click on a link, eg cat and the page appears
                       like.



                     • Click on dog and the page appears like.


                                                                      71




                    Sending data with Hyperlinks 3
                     • Create a page and save it as
                       animal_question.php and create it to
                       look like thi
                       l k lik this.




                     • The links are to the page
                       animal_home_page.php


                                                                      72




Database php version 2.1                                                          36
28/06/2009




                  Sending data with Hyperlinks 4
                     • The links on the animal_question.php
                       p g
                       page should look like..
                     • Cat



                     • Dog
                         g



                                                               73




                    Sending data with Hyperlinks 5
                     • Another way to set the link with
                       p
                       parameters is, click on the browse
                       icon select the file and click on the
                       parameters buttons as shown.




                                                               74




Database php version 2.1                                                   37
28/06/2009




                   Sending data with Hyperlinks 6
                     • The animal_home_page.php should
                       look like.




                     • Create the URL Variable as before
                       with the binding panels.


                                                                    75




                                  Cookies - 1
                     • The form and querystring request are
                       stateless variables. Once the server has
                       passed th
                            d them onto th client the server has
                                       t the li t th          h
                       no memory of them.
                     • Cookies are tiny text files that are
                       written onto the user’s hard drive.
                     • Cookies help to maintain state over coming
                       the t t l
                       th stateless HTTP protocol.
                                               t    l



                                                                    76




Database php version 2.1                                                        38
28/06/2009




                                          Cookies - 2
                      • Cookies and security
                           – Cookies can’t be used to infect computer
                                                                p
                             with a virus.
                           – Cookies can only be read by the site
                             that sent them.
                           – Cookies should never contain credit card
                             details, password, etc.
                                    ,p         ,




                                                                             77




                                          Cookies - 3
                   • Open the test_form_processor_get.php file .
                   • Create a new page test_form_processor_cookies.php
                   • Add the following to the test_form_processor_get.php
                     file.
                     file


                  Cookie variable
                  name




                               This code must be place before the html tag   78




Database php version 2.1                                                                 39
28/06/2009




                                     Cookies - 4
                     • Create the bindings and place them in
                       the design view of the
                               g
                       test_form_processor_cookies.php
                       file.
                     • As shown, then save and preview.




                                                                     79




                                         Lab 1
                     • Objectives
                     • After completing this lab, you should
                                  p   g           y
                       be able to:
                           – Create URL variables in the Bindings
                             panel.
                           – Display URL variables on a page.
                           – Create FORM variables in the Bindings
                             panel.
                                 l
                           – Display FORM variables on a page.


                                                                     80




Database php version 2.1                                                         40
28/06/2009




                                Lab 1 cont-2
                   • Copy the folder lab_start from the x
                     drive to your working drive.
                              y          g
                   • Create a new site called lab
                   • Setup the folders on the IIS root folder
                     and the remote and testing servers.




                                                           81




                                                           82
                            unit2sendingpage.php




Database php version 2.1                                               41
28/06/2009




                           unit2formactionpage.php

                                                                     83




                            unit2urlactionpage.php?firstname=Larry



                                                                     84




Database php version 2.1                                                         42
28/06/2009




                                   Chapter 2
                            Sending form details via emails




                                                              85




                           PHP Sending E-mails
                     • PHP allows you to send e-mails
                       directly from a script.
                              y            p
                     • The PHP mail() Function
                     • The PHP mail() function is used to
                       send emails from inside a script.




                                                               86




Database php version 2.1                                                   43
28/06/2009




                                           Syntax




                     Note: For th
                     N t F the mail f il functions t b available, PHP requires an
                                             ti    to be   il bl           i
                       installed and working email system. The program to be used
                       is defined by the configuration settings in the php.ini file.
                       Read more in our PHP Mail reference.


                                                                                       87




                              PHP Simple E-Mail
                     • This can be used to check if the
                       server is setup correctly.
                                     p         y




                                                                                       88




Database php version 2.1                                                                           44
28/06/2009




                               Sending Emails - 1
                     • 1st check that SMTP is installed.
                       Through the window component
                              g                  p
                       wizard. Highlight IIS and click on
                       details.
                     • If the SMTP is ticked then it is
                       installed if not you need to install it.




                                                                        89




                               Sending Emails - 2
                     • Overview:
                           – In this task you will create a message
                                          y                      g
                             sent page that will transfer the data to
                             an email.
                           – The page that obtains the email data is
                             the contact.php page. You will add a
                             table with no borders and a form to
                             send the information to the
                             message_sent.php page.


                                                                        90




Database php version 2.1                                                            45
28/06/2009




                           Sending Emails - 3
                     • Open the generic_template.php and
                       save it as message_sent.php
                                       g       p p
                     • Looks like this




                     • Make it look like this, with link to
                       index.php.

                                                              91




                           Sending Emails – 4
                     • Add the php code as shown.




                                                              92




Database php version 2.1                                                  46
28/06/2009




                                    Sending Emails - 5
                     •   Open contact.php and change it to look like this. Details on the
                         next slide.




                                                                                            93




                                  Sending Emails - 6
                     • Position the insertion point below the image
                       caption, and choose Insert
                           > Form > Form to create a new form.
                     • Without moving the insertion point, choose Insert
                       > Table, providing
                           the following settings. Click OK.
                           –   Rows: 4
                           –   Columns: 2
                           –   Width: 95 Percent
                           –   Border: 0
                           –   Cell Padding: 3
                           –   Cell Spacing: 0



                                                                                            94




Database php version 2.1                                                                                47
28/06/2009




                                 Sending Emails - 7
                     • Select the <table> tag using the tag selector, and
                       use the Property inspector to change its bgcolor
                       (background color) attribute to #eeeeee, which is
                       a light gray
                               gray.
                     • In the right column of the table insert the
                       following;
                           – Two text fields called,
                              • emailAddress
                              • subject
                           – One text area called body
                           – A Submit button
                     • In the first three cells of the left column, enter
                       the following text:
                           – Your Email Address
                           – Subject
                           – Message Body
                                                                            95




                                 Sending Emails - 8
                     • Select the text area and give it the
                       following specifications;
                           – Char Width :55
                                          55
                           – Num Lines as 6, and Wrap as Virtual.
                     • Select the form in the tag selector, in the
                       Property inspector set:
                           – Name = frm_message
                           – Action = messageSent.php
                           – Method = POST
                     • Insert a Heading 2 element above the
                       table that reads, “Send Us a Message.”


                                                                            96




Database php version 2.1                                                                48
28/06/2009




                    Client-Side Form Validation
                     • Open contact.php
                     • Select the submit
                                  subm t
                       button
                     • In the behaviours
                       panels add a
                       behaviour as
                       shown.
                        h



                                                                  97




                    Client-Side Form Validation
                     • In the Validate form dialog as shown
                       set the fields as
                           – emailAddress: Required as an email
                           – Subject: Required as anything
                           – Body: Required as anything.
                     • Resulting with the dialog shown
                       below.
                       b l



                                                                  98




Database php version 2.1                                                      49
28/06/2009




                   Client-Side Form Validation
                     • Notice the behaviours
                       p
                       panel should look like
                       this when you
                       highlight the submit
                       button.
                     • Save and test the
                       validation.
                       validation



                                                                       99




                                       Lab 2 - 1
                     • Overview:
                           – Create a html form with client-side
                             validation
                           – Create an action page to send an email.
                     • Your overview instructions located in
                       the next section of the book: lab
                       section.
                       section
                     • Your two pages will look like the
                       following 2 slides.
                                                                       100




Database php version 2.1                                                            50
28/06/2009




                              Sendemail.php




                                                  101




                           Sendemail_action.php




                                                  102




Database php version 2.1                                       51
28/06/2009




                           Sample of email sent




                                                                     103




                                    Lab 2 - 1
                     • Open server_side_scripting site
                     •  Open tourtemplate.php.
                     • Change to Split view
                                        view.
                     • In the Design view pane, click on the link
                       Or email us.
                     • In the Property inspector change the link
                       to link to sendemail.php. Hit the enter key
                       after putting in the link (if you don’t hit
                       enter,
                       enter the value will not be saved).
                                                     saved)
                     • Save tourtemplate.php.


                                                                     104




Database php version 2.1                                                          52
28/06/2009




                                               Lab 2 - 2
                     • Create an Email Form Page
                           – Save tourtemplate.php as sendemail.php.
                           – Replace the generic left heading with Send
                             Email.
                           – Replace the generic Left Side Text with the
                             text “Fill in the values in the form and submit
                             the form to send an email to us. Thank you for
                             your interest.”
                           – Create a new form below the text.
                               • Name the form email.
                               • Set the action to sendemail_action.php.
                               • Make sure that the Method is Post.




                                                                                   105




                                               Lab 2 - 3
                     • Create a table inside the form with 2 columns and 4 rows.
                           – Type in the labels as shown in the table below.
                               • From Address
                               • Subject
                                    j
                               • Message Body
                           – In the second column,
                               • first row put a text field and name it from
                               • With a width of 30 and max of 50.
                           – In the second row,
                               • put a text field and name it subject
                               • with the same width and max.
                           – In the third row,
                               • put a textarea control and name it message
                                 p                                       g
                               • with a width of 40 and 10 rows.
                           – In the last row,
                           – first column put a submit button control
                           – with a label of Send Email.


                                                                                   106




Database php version 2.1                                                                        53
28/06/2009




                                           Lab 2 – 4
                     • Create Action Page
                           – Create a new page from tourtemplate.php
                              • name : sendemail_action.php.
                                                        p p
                           – Note that the email link now points to sendemail.php.
                     • Change the left section title to Email Complete
                       and the text to “Thank you for your email.”
                     • In bindings, create 3 new Request variables:
                           – from
                           – subject
                           – message




                                                                                     107




                                           Lab 2 - 5
                  • In Code view make space between
                    the heading and the text Type
                                        text.
                    the following php code.




                                                                                     108




Database php version 2.1                                                                          54
28/06/2009




                                           Lab 2 - 6
                     • Replace your email address with mine.
                       The From address would normally be
                                                        y
                       hard coded with the site’s email
                       address.
                     • Save the page.
                     • Preview the sendemail.php page. Fill
                       out the form and submit it using your
                         t th f       d b it        i
                       email address as the sender.

                                                                                       109




                                           Lab 2 - 7
                     • Client Side Validation
                           – Return to Dreamweaver and the sendemail.php page.
                           – Select the Submit button.
                           – In the Behaviours panel, click Add Behaviour and choose
                             Validate Form.
                           – Make all of the fields are required and validate the
                             email address.
                     • Save the page.
                     • Preview sendemail.php.
                     • Check that validation is working by trying to
                       submit an empty form and also try an invalid email
                       address format.


                                                                                       110




Database php version 2.1                                                                            55
28/06/2009




                                   Chapter 3
                                 Self-
                                 Self-server application
                                           &
                                 Server-
                                 Server-side validation



                                                             111




                           Self-server Applications
                     • These applications are answering a
                       q
                       question that would usually be
                                                   y
                       answered by the staff in the tours
                       company.
                     • An example is how much does it cost
                       to do this tour and another tour.
                     • W will create a tours calculator,
                       We ill       t   t         l l t
                       (self-server application).

                                                              112




Database php version 2.1                                                   56
28/06/2009




                    The tour calculator page - 1
                     • Open the generic_template.php and
                       save as tourprice_processor.php and
                                   p     p         p p
                       tourprice.php.
                     • The two pages are shown on the next
                       slides.




                                                                  113




                       The tour calculator page - 2




                                              Objects names:
                                              txtNumberAdults
                                              txtNumberChildren
                                              cboTourName


                           tourprice.php
                                                                  114




Database php version 2.1                                                       57
28/06/2009




                       The tour calculator page - 3




                                                               Calculate link to
                      Contact link to contact.php
                                                               tourprice.php
                                                               page

                                     tourprice_processor.php                       115




                     The tour calculator page - 4




                                                                                   116




Database php version 2.1                                                                        58
28/06/2009




                       The tour calculator page - 5
                     • tourprice.php has the following properties.
                           – Form
                              • Name = frm_tourprice
                                        frm tourprice
                              • Action = tourprice_processor.php
                              • Method = POST
                           – Table inside the form
                              • 4 rows
                              • 2 columns
                              • width of 60 percent
                           – List values
                              • Highlights of Argentina, 500
                              • Highlights of Western Canada, 700
                              • Egyptian Pyramids and More, 900

                                                                                     117




                           Calculating the price - 1
                     • Open tourprice_processor.php page.
                     • Input the php code as shown to;
                           – declare and initialise the number of adults, kids
                             and tour name.
                           – Calculate the price of the tour.

                                                                     Variable declaration




                               Check you have the same variable
                               names in your tourprice.php page as
                               those in quotes.
                                                                                     118




Database php version 2.1                                                                           59
28/06/2009




                           Calculating the price - 2
                • Now you have calculated the tourprice
                  variable, you need to display it. This is done by
                  placing php script amongst the html code as
                                                         code,
                  shown below.
                • Save and view the page. What happens when
                  you don’t put in an amount for the adults or
                  kids?




                                                                      119




                           The calculator so far….
                     • So far the calculator works if
                       numeric values are put in the adults
                                           p
                       and kids fields.
                     • Errors appear if not values are
                       placed in the adults and kids fields.
                     • The next couple of slides are going to
                       add a css rule f errors and trap
                        dd         l for           dt
                       the error with php code.

                                                                      120




Database php version 2.1                                                           60
28/06/2009




                       Adding new style to the css
                     • Add an error style to the css file with the
                       following properties;
                           –   Type: class
                           –   Name: error
                           –   Weight : bold
                           –   Colour : #990000
                     • This style will be used if the client makes
                       an error with the information for the tour
                                  ith th i f       ti f th t
                       calculator.


                                                                     121




                           Server-side validation -1
                     • Server-side validation is validating the
                       tourprice.php page variables sent to the
                       tourprice_processor.php page.
                     • This is done by checking the two form
                       variables for numerical values, as shown.
                     • If one of them doesn’t hold a numeric
                       value the client is sent back to the
                       tourprice.php page, displaying an error.
                     • Add the code
                                code.



                                                                     122




Database php version 2.1                                                          61
28/06/2009




                           Server-side validation- 2
                  • Once the client is sent back to the
                    tourprice.php page the code to pick up this
                    error is needed
                             needed.
                  • This code shows the response to that
                    variable and displays a message to the client.




                                                                            123




                                        Lab 3 - 1
                     • In this lab you are to add a list/menu to
                       reservations.php and change the form action to
                       call the page reservations_action.php.
                                p g                       p p
                     • The two pages are shown on the next slides.
                     • To have “Yes” or “No” displayed on equipment you
                       will have to write an if statement finding out if
                       the box was ticked, “true” or not ticked, “false”.

                               If request.form(“equipment”)=“true” then
                                        response.write(“No”)
                                                      (“   )
                               Else
                                        response.write(“Yes”)
                               End If

                                                                            124




Database php version 2.1                                                                 62
28/06/2009




                                         Lab 3 - 2




                                                              The value for each label
                                                              is: 1 for Nepal Track, 2
                                                              for Alasken Wilderness,
                                                              etc.
                            reservations.ph                                              125
                            p




                                         Lab 3 - 3




                                                             Reservation Information
                                                             from reservation.php page

                           reservations_action.php. This page was created
                                                                                         126
                           form tourtemplate.php.




Database php version 2.1                                                                              63
28/06/2009




                                       Chapter 4
                              Introduction to Databases in PHP




                                                                 127




                                      Overview
                     • In this chapter you will :
                           – Create a database connection in
                             dreamweaver.
                           – Create a recordset.
                           – Display data from the recordset.




                                                                  128




Database php version 2.1                                                       64
28/06/2009




                 Placing the database on the server
                     • We are going to using the database
                       y
                       you created in the sql component:
                                           q     p
                       newlands.
                     • Make sure this is database is in the
                       sql directory as shown below.




                                                               129




                           Exploring the Database
                     • Open the database called
                       newland_tours.mdb.
                     • This is the database that you will be
                       working with.
                     • The relationships look like this.




                                                               130




Database php version 2.1                                                    65
28/06/2009




                     Connecting the site to a database
                     • Select the + as shown.
                     • The following sql
                            follow ng
                       connection appears,              Note the
                       select it.                       database
                                                        tag has

                     • This will produce a dialog       been
                                                        selected.
                       as shown.

                    Complete the dialog as shown.
                    Test the connection and click
                    OK


                                                              131




                            Check the Connection
                     • Click on the + and explore
                       the tables etc.

                     • Note a folder has been added to your
                       site with the connection php file.
                     • This file is a Dreamweaver
                       configuration.


                                                             132




Database php version 2.1                                                   66
28/06/2009




                   What’s in the connection file?




                                                     133




                           Displaying data Using a
                                recordset- 1
                     • Open the
                       index.php file
                             p p
                       in design view
                       and delete the
                       highlight text.




                                                     134




Database php version 2.1                                          67
28/06/2009




                            Displaying data - 2
                     • Select the traveller’s journal image and
                       right click on the h3 tag in the tag selector.
                     • Select the remove tag from the pop up
                       menu.




                                                                   135




                            Displaying data - 3
                     • Select               Creating a recordset

                       the + in
                       the
                       th
                       bindings
                       as shown.
                     • Fill in
                       the
                       dialog as
                       shown.


                                                                   136




Database php version 2.1                                                        68
28/06/2009




                               Displaying data - 4
                  • Drag and drop
                    description as
                    shown.
                    shown
                  • Looks like
                  • You can also view Data
                    using the live data as
                    shown




                                                                     137




                                       Lab 4 -1
                     • Overview
                           – Create sql connection for the lab
                                     q
                             database
                           – Create a dreamweaver connection
                           – Create a recordset for the equipment.
                           – Create a dynamic table using the
                             equipment recordset
                                        recordset.




                                                                     138




Database php version 2.1                                                          69
28/06/2009




                           Lab 4 equpment.php Design




                               Dynamic table



                                                       139




                           Lab 4 equipment.php live




                                                       140




Database php version 2.1                                            70
28/06/2009




                                             Lab 4 -2
                     • Connect the site to the database
                           – call it dwda.
                     • Open equipment.php page.
                     • Create a recordset
                           – Call it rs_equip
                           – Select all field except EquipID
                           – Sort on EquipType field in Ascending order.
                     • Test the recordset.



                                                                           141




                                  Lab 4- recordset




                                                                           142




Database php version 2.1                                                                71
28/06/2009




                                            Lab 4 -3
                     • Select the HTML table containing equipment and
                       delete the table.
                     • On the Application tab on the Insert bar, click
                       Dynamic Data and choose Dynamic Table.
                     • Choose all records from the rs_equip recordset
                       and give a border of 1 and a cellpadding of 3.
                     • Click OK.
                     • Change the headings to be more descriptive and
                       bold them.
                     • Save the page and preview it.




                                                                                      143




                                            Lab 4 -4
                     • Format the price column
                           – Select the binding on the page for the price column.
                           – In the Bindings panel, the price column will be
                                          g p     ,     p
                             highlighted. Drop down the Format column’s select box
                             and choose Currency > Default. Refer to next slide for
                             menus.
                     • Note: You may not see the Format column in the
                       Bindings panel if it is
                       too narrow. You can expand the panels or scroll
                       within the Bindings
                       panel.
                            l
                     • Preview the page. You should see the same list of
                       equipment now coming from the database.


                                                                                      144




Database php version 2.1                                                                           72
28/06/2009




                           Formatting numbers.




                                                        145




                                 Chapter 5
                              Dynamical Calculations




                                                       146




Database php version 2.1                                             73
28/06/2009




                                        Overview
                     • In this chapter you will:
                           – Dynamically populate a drop-down menu.
                              y        yp p            p
                           – Create a recordset using a filter from
                             the form data on the previous asp page.
                           – Insert dynamic text.
                           – Write the code to handle errors when
                             data has not validated
                                          validated.




                                                                               147




                     Dynamically populated drop-
                            down menus.

                                                      The next activity will
                                                      create a drop-down
                                                               d    d
                                                      menu which is
                                                      populated from the
                                                      database through a
                                                      recordset.




                                  tourprice.php


                                                                               148




Database php version 2.1                                                                    74
28/06/2009




                           Dynamic calculation
                     • Remember the Tour Calculator.
                     • We are going to change it to dynamically
                       put the tour locations in the list/menu,
                       (with the ID being the value) on the
                       tourprice.php page.
                     • On the tourprice_processor.php, we will
                       then use the id to select the adult and
                       child rate t calculate th cost or the
                        hild t to l l t the            t   th
                       tour.


                                                                    149




                   Create a tourprice recordset

                                                        Test the recordset


                                                        View the sql
                                                        statement with the
                                                        advanced button.




                                                                    150




Database php version 2.1                                                            75
28/06/2009




                           Dynamic drop-down
                     • Open tourprice.php page and select
                       the list/menu.
                     • Select the dynamic button.
                     • Remove the static labels and values
                       with the – button.




                                                             151




                           Dynamic drop-down-2
                   • Fill the
                     dialog as
                           g
                     shown.




                                                             152




Database php version 2.1                                                  76
28/06/2009




                           Dynamic drop-down-3
                     • Save and view as shown.




                                                                           153




                 Next Activity: Creating dynamic text
                                               Dynamic text




                                                 tourprice_processor.php

                                           Click on the submit button
                                                               button.
                                           The data in the form is
                                           transferred to the
                           tourprice.php   tourprice_processor.php page.
                                           The price is calculated using the
                                           form data.
                                                                           154




Database php version 2.1                                                                77
28/06/2009




                               Dynamic calculation
                     • Open tourprice_processor.php page.
                     • Create a new recordset (shown on
                       next slide), with the following
                       attributes:
                           –   Name = rsTourpricesFiltered
                           –   Connection = conn_newland
                           –   Table = tours
                           –   Columns = selected tourID, tourName,
                               basePriceAdult, basePriceChild


                                                                                    155




                                Filtered recordset
                  • Click the Test
                    button. Enter 9 in
                    the Please P id
                    th Pl       Provide
                    a Test Value
                    dialog, and click
                    OK.
                  • Click OK to exit
                    the test output
                              output,
                    and again to save
                    the recordset.      Note: This recordset has a filter on
                                             it. The recordset will only select a
                                             tour id that equals tour is that was
                                             selected from the pervious asp page.   156




Database php version 2.1                                                                         78
28/06/2009




                           Displaying dynamic text
                   • Place xx as shown.
                   • Highlight xx.
                       g g
                   • Select Dynamic Text
                     as shown.
                   • The dialog shown
                     appears, select
                     tourName and click
                     OK.


                                                            157




                             Fixing up the price.
                     • Delete the code that sets the base
                       p
                       price.



                     • Add the following lines




                                                            158




Database php version 2.1                                                 79
28/06/2009




                                Avoiding errors
                     • Add the following to avoid
                       calculation errors.



                     • Save and view the pages.




                                                                       159




                                      Lab 5 - 1
                     • In this lab you will;
                           – Create a dynamic list/menu for the
                                       y
                             reservation.php page, shown on the next
                             slide.
                           – Add a dynamic feature tour list, as
                             shown on the following slides.




                                                                       160




Database php version 2.1                                                            80
28/06/2009




                                     Lab 5 - 2




                                                          Dynamic tour list
                                                          •Note: Duplicate
                                                          entries because
                                                          the same trip is
                                Reservation.php           offered on
                                                          different dates.    161




                           Lab 5 – 3 tourtemplate.php




                                                  Static Featured tours




                                                  Dynamic Feature tours
                                                                              162




Database php version 2.1                                                                   81
28/06/2009




                                       Lab 5 - 4
                      • reservations.php page.
                      • create a recordset
                           – named rs_tours from th tours table.
                             nam rs tours         the      ta .
                           – Select only TourID, TourName, and TourDate
                           – sort by TourName.
                      • Click the Dynamic button on the Property
                        inspector.
                           – Dynamically fill the values from the recordset
                             that you just created. The values are the TourID
                                           created
                           – Labels are the TourName
                           – Click OK to close the dynamic dialog.
                      • Save the page and preview it. Drop down the
                        list to see the dynamic values.          163




                                       Lab 5 - 5
                     • Create a recordset
                     • Delete the text under
                       the Featured Tours
                       heading.
                     • Add a dynamic table
                       as shown. Remember
                       border=0. Save and
                       view.




                                                                          164




Database php version 2.1                                                               82
28/06/2009




                                      Chapter 6
                                 Filtering & Displaying Data




                                                                 165




                                      Overview
                     • In this chapter you will:
                           – Transfer dynamic data through the URL.
                                        y                g
                           – Add repeating regions.
                           – Creating recordsets with information
                             from the URL.
                           – Adding images dynamically.




                                                                  166




Database php version 2.1                                                       83
28/06/2009




                           Changing profiles.php
                     • The profiles page displays the countries
                       statically.
                     • We are going to change this to dynamically
                       from the database.




                                                                    167
                            Profiles current page




                           Profiles_details.php
                     • Create a profiles_details.php page
                       from the generic template.
                                g           p




                                                                    168




Database php version 2.1                                                         84
28/06/2009




                              profiles.php - 1
                     • Delete the countries so the
                       p
                       profiles.php page is as shown.
                                p pp g




                                                        169




                             profiles.php - 2
                     • Create a recordset as shown.




                                                        170




Database php version 2.1                                             85
28/06/2009




                              profiles.php - 3
                     • Insert a country
                       name field binding,
                       as shown.
                     • With this insertion
                       highlighted click on
                       the browse icon for
                       a link.
                     • Filling the browse
                       as shown.
                     • Select the
                       parameters button.


                                                                       171




                              profiles.php – 4
                                              • In the parameters dialog
                                                box, click under the name
                                                column heading and type
                                                the names as shown.
                                              • Click on the lighting bolt
                                                and select as shown for
                                                both values.
                                              • This parameters button
                                                writes the url code in the
                                                link.
                                                li k
                                              • Here we are passing the
                                                country ID and name in
                                                the URL.

                                                                       172




Database php version 2.1                                                            86
28/06/2009




                                  profiles.php – 5
                     • Notice now the binding is a link.


                     • Notice the link sends the id of the country
                       in the URL.


                     • With the link highlighted
                           – In the server behaviours
                           Panel click + and choose
                           Repeat Region, as shown.


                                                                 173




                                  profiles.php – 6
                     • Adding a repeat region and click on
                       OK.



                     • A section page of the page is shown.
                       Check that the break is inside the
                       repeat region


                                                                 174




Database php version 2.1                                                      87
28/06/2009




                                     profiles.php – 5
                     • View the page. It should look like




                                                                                         175




                                profiles_details.php
                     •    Open the profiles_details.php
                     •    Create a recordset and complete the page to look like below.
                     •    The following slide have the details on how complete it.




                      Heading 1



                         Heading 2


                     process_details.php

                                                                                         176




Database php version 2.1                                                                              88
28/06/2009




                           Create the recordest




                                    Click the advanced.. button.
                                                                   177




                           Combining two tables
                    • Add the other table; region.
                    • Add the where clause to join the
                      tables.
                      t bl




                                                                   178




Database php version 2.1                                                        89
28/06/2009




                             Add Bindings & View
                     • Add the bindings as shown on the pervious slides.
                     • The finished page should look like:




                                                                           179




                           Inspecting the pervious sql
                     • Take a look at the Sql
                     • What does it mean
                                   t




                                                                           180




Database php version 2.1                                                                90
28/06/2009




                           Dynamic images -1
                     • Now we are going to add the image
                       that go with the country.
                            g                 y
                     • Place the cursor as shown.




                     • From the menu select insert->image.


                                                             181




                           Dynamic images -2
                  • Select as
                    shown and
                    click OK
                     li k OK.
                  • Create the
                    imageright
                    css rule and
                    apply it to
                    the image.


                                                             182




Database php version 2.1                                                  91
28/06/2009




                            Dynamic images -4
                     • Completed page.




                                                                    183




                                    Lab 6 -1
                     • Dynamically creating a repeated region for
                       the tour packages and their dates.
                     • Then the tour ID is sent in the URL to
                       open the tourpackages.php page.
                     • The tourpackage.php page uses the tour
                       ID to display the info about the selected
                       tour package.
                     • The following slides show the changes of
                       the two pages.


                                                                    184




Database php version 2.1                                                         92
28/06/2009




                                   Lab 6 -2
                     • What the page look like now.




                                tours.php
                                                             185




                                   Lab 6 -3
                     • What the page will look like at the
                       end




                                               tours.php     186




Database php version 2.1                                                  93
28/06/2009




                                     Lab 6 -4
                  • Current
                    page




                                 tourpackages.php                      187




                                      Lab 6 -5
                     • What the page will look like at the end




                                                    tourpackages.php   188




Database php version 2.1                                                            94
28/06/2009




                                       Chapter 7
                                SQL with Joins & Recordset
                                          Paging



                                                                    189




                                      Overview
                     • In this chapter you will:
                           – Use the SQL term INNERJOIN.
                                       Q
                           – Adding a recordset navigational tool bar.




                                                                     190




Database php version 2.1                                                          95
28/06/2009




                              tours_detail.php - 1
                     • For this activity you are going to;
                           – create a page that joins the information
                                      p g       j
                             from two tables on a set of criteria.
                           – Add a recordset paging object.
                     • The page is shown on the next two
                       slides.




                                                                        191




                           Part 1 of tours_details.php




                                                                        192




Database php version 2.1                                                             96
28/06/2009




                           Part 2 of tours_details.php
                     • Adding recordset Paging




                                                                    193




                              tours_detail.php - 2
                     • Open generic_template.php
                           – Title: Newland Tours: Tour Descriptions.
                                                              p
                           – Save as tour_details.php.
                     • Make the following changes to the
                       page as shown.
                            Heading 2
                                  g
                            Link to tours.php




                                                                    194




Database php version 2.1                                                         97
28/06/2009




                           tours_detail.php - 3
                     • Create a recordset as follows;




                                                                                   195




                           tours_detail.php - 4
                                                                       Dynamic image




                                                  Table width=60%




                                                     For these links refer
                                                     to following slides.




                           Using the recordset add the bindings and the repeat
                           region ( 5 records at a time), to the page as shown.
                                                                                   196




Database php version 2.1                                                                        98
28/06/2009




                           tours_detail.php - 5
                     • The link “Learn more about” has the
                       following details.




                                                             197




                           tours_detail.php - 6
                     • Price this tour with Tour price
                       calculator.




                                                             198




Database php version 2.1                                                  99
28/06/2009




                            tours_detail.php - 7
                     • Insert the dynamic image’s placeholder
                       and set it right align.




                                                                199




                           Adding a repeat region
                     • Highlight all the recordset data.
                     • Select the server behav our tab.
                                          behaviour
                     • Click on the plus symbol and select
                       repeat region.
                     • Select five records at a time, as
                       shown below.



                                                                200




Database php version 2.1                                                    100
28/06/2009




                         Part 2
                  Recordset Navigation-1

                   • Inserting a
                     recordset
                     navigation can
                     be done by the
                     following two.




                                                            201




                           Recordset Navigation-2
                     • When you add a navigation the
                       recordset dialog appears as shown.
                                      g pp




                     • Save and preview.
                                p



                                                            202




Database php version 2.1                                                101
28/06/2009




                    Fixing up the tourprice.php page
                     • Create a querystring variable as
                       shown below.



                     • Click to select the drop-down menu in
                       the form, beside Tour name. In the
                       Property inspector, click the Dynamic
                       button. Shown on the next slide.

                                                               203




                                 Dynamic list




                              Click on the lighting bolt and
                              the Dynamic Data dialog box
                              appears.                         204




Database php version 2.1                                                   102
28/06/2009




                                  Lab 7 -1
                     • This lab creates a page called
                       bookedtours.php.
                                    p p
                     • The tour bookings information is
                       shown on the next slide.




                                                          205




                                  Lab 7 -2




                                                          206




Database php version 2.1                                              103
28/06/2009




                                  Chapter 8
                                Dynamic searching




                                                           207




                           Creating a search.
                     • In this chapter you are going to
                       create sql statements that search
                                q
                       for information.
                     • The next slides show the changes to
                       the tours.php page, tours_details.php
                       page and index.php page




                                                            208




Database php version 2.1                                                104
28/06/2009




                              tours.php




                                               209




                           tours_details.php




                                               210




Database php version 2.1                                   105
28/06/2009




                                  index.php




                                                           211




                           Preparing the search page.
                     • Make the changes to the tours.php
                       p g
                       page as shown on the next page.
                                                 p g




                                                           212




Database php version 2.1                                               106
28/06/2009




                                   tours.php page
                1.Change text
                Link to
                tourprice.php



                2. Insert table



                Recordset for
                the dynamic
                list values
                     values.
                                  A recordset   A form with the
                                  shown on      following details
                                  next page.



                                                                    213




                           Creating the recordsets -1.




                                                                    214




Database php version 2.1                                                        107
28/06/2009




                           Creating the recordsets -2.




                                                         215




                                 Dynamic list




                                                         216




Database php version 2.1                                             108
28/06/2009




                  Creating the repeating section




                                                                                  217




                                   Region Link




                           The filtering on the link would work until we change
                           the query on the tour_details.php page which is
                           done later.                                            218




Database php version 2.1                                                                      109
28/06/2009




                               Now fix the index.php



                Profiles.php
                link
                Contact.php
                link.

                                      A recordset and a repeat
                                      region. This is the same as the
                                      world region recordset in the
                                      tours.php page.

                                                                                   219




                  h1
                               tours_details.php page
                  h2                                               Image shown on
                                                                   the following pages

                Recordset
                shown on the
                following
                slides




                 Insert a                                               Link to
                                                                        Li k t
                 table                                                  toursprice.php
                                                                        shown on the
                                                                        next page



                                                                                   220




Database php version 2.1                                                                       110
28/06/2009




                           Tour price calculator link




                                                        221




                            Tour details recordset




                                                        222




Database php version 2.1                                            111
28/06/2009




                              Image details




                                                          223




                     Adding a recordset navigation
                     • Add a recordset navigation to the
                       bottom of the tours_details.php page
                       as shown
                          shown.
                     • Add the find Tours link to tours.php
                       page




                                                          224




Database php version 2.1                                              112
28/06/2009




                               Looking at the code
                    Add this code to tours_details.php
                                                                 When you select view all
                                                                 countries from the
                                                                 tours.php page this is the
                                                                 recordset you what to use.




                                                                        When you click on
                                       If you select a country          the region link
                                       from the tours.php page          this is the
                                       then this is the                 recordset to use
                                       recordset to use.
                                                                                      225




                   Adding an if in case there are
                            no records
                                                                    Add these lines




                                                                                      226




Database php version 2.1                                                                            113
28/06/2009




                                  Lab 8 Overview
                     • In this lab you will:
                           – Create a search interface by creating a form
                             and allowing the user to select a value from a
                             list/menu object and returning the tours that
                             match the selected value.
                     • Objectives:
                           – Group data in a select statement
                           – Create a search page from a form
                           – Implement a search results page using the
                             value the user selected on the form.
                           – Add conditional logic to the SQL statement.


                                                                              227




                           You will create this page




                                       Searchreservations.php
                                                                              228




Database php version 2.1                                                                  114
28/06/2009




                             Receiving data from
                           searchreservations.php




                               bookedtours,.php
                                                               229




                                      Chapter 9
                             Using Session variables, hidden
                                 fields & inserting data.




Database php version 2.1                                                   115
28/06/2009




                                          Overview
                     • In this chapter you will
                           –   Create restricted administration pages.
                                                                p g
                           –   Use Session variables
                           –   Use hidden fields
                           –   Insert records into a table.




                  Creating an Insert Journal Pg -1




                                      Open the generic_template.php.
                                      Save as journalAdd.php.
                                      Change the page as shown above.




Database php version 2.1                                                       116
28/06/2009




                                  Insert a record

                    • On the
                      journalAdd.php
                      j     lAdd h
                      page and select the
                      “Insert Record” as
                      shown.
                    • The next slide
                        he     sl de
                      shows the screen
                      that appears.




                               Inserting a record




                   • Notice that the JournalID field needs a value.
                   • This value needs to be increased by one.
                      – Eg: last value is 3 the next value needs to be 4.




Database php version 2.1                                                          117
28/06/2009




                  Adding a recordset to find the id value
                     • Create this recordset to find the
                       largest number.




                       Creating a new journal ID




                    • Find the recordset code as shown
                      above.
                    • Add lines 61 & 62
                           li        62.
                    • These lines obtain the highest id and
                      add one to it.




Database php version 2.1                                            118
28/06/2009




                      Storing the id in the form.
                  • Add a hidden
                    textfield in the
                    form as shown
                             shown.
                  • Write the value as
                    shown. This is the
                    value of the new id
                    that was
                    calculated at the
                    top of the page on
                    the previous slide.




                  Placing the insert record script
                     • Double click on the insert record
                       made earlier and add the new id as
                       the journalID.




Database php version 2.1                                          119
28/06/2009




                             Session Variables




                      Creating a Session variable
                     • Create a basic page as shown with a
                       textfield and button in a form.




                     • Create a new page and call it
                       createsession.php.
                     • Link the form to the
                       createsession.php page.




Database php version 2.1                                           120
28/06/2009




                   Creating a Session variable - 2
                    • To create a session variable the
                      session_start() function must be the 1st line in
                             _     ()
                      the document.
                    • Then declare and initialise the variable as
                      shown below. Post from the page before.




                   Creating a Session variable - 3
                    • Create the page below, starting the session
                      and then displaying it.
                                   p y g
                    • Create a link on the createsession.php to this
                      page. This is show on the previous slides.




Database php version 2.1                                                       121
28/06/2009




                                    Classes in php




                                Creating A Class
                    • Classes are similar to Java and actionscript.
                    • A class has a constructor or a default on is added at
                      compilation time.
                    • Below is the start of a person class.
                    • This class is to hold the info about the people that log
                      into the following pages.
                                                      Name of the class and file
                                                      name must be identical.
                                                      Declaring
                                                      D l i variables.
                                                                i bl
                                                      Constructor must be identical
                                                      to the class name.




Database php version 2.1                                                                    122
28/06/2009




                           Creating a Class -2
                   • The gets and
                     the sets.
                   • The close of
                     the class
                   • The close of
                     the php tag.




                   Using the class to store variables
                     • The next couple of slides show the
                       code to create a session variable so
                       it can store an array of objects.
                     • These objects are created using the
                       Person class.




Database php version 2.1                                            123
28/06/2009




                  Using the class to store variables-2
                     • A login page to start the session
                       variable.




                  Using the class to store variables-3
                     • The design view of the add people
                       p g
                       page.




Database php version 2.1                                         124
28/06/2009




                  Using the class to store variables-4
                   • The php code.                 The include
                                                   statement allows
                                                   access to the Person
                                                   class.
                                                    l




                  Using the class to store variables-5

                    • The html code on the page.




Database php version 2.1                                                        125
28/06/2009




                  Using the class to store variables-6

                    • How to view the data in the class.




                  Using the class to store variables-7
                                                  Table headings.
                                                  Obtaining the number of
                                                  objects in the array.
                                                  Getting each variable for
                                                  each object.
                                                  Placing each variable in
                                                  the table




Database php version 2.1                                                            126
28/06/2009




                  Using the class to store variables-8
                     • These files can be found a zipped
                       folder called classes.zip on
                                               p
                       myChisholm.




Database php version 2.1                                         127

More Related Content

PDF
Scalable Internet Servers and Load Balancing
PDF
The Australian web domain harvests: a preliminary ...
PDF
Xen e OpenVirtuozzo
XLS
It Appropriation Request
PPTX
The Megasite: Infrastructure for Internet Scale
DOC
Perl 1997 Paper
PDF
Filling The Gap - Going Mobile With JBoss Technologies Today
PPT
Css 2010
Scalable Internet Servers and Load Balancing
The Australian web domain harvests: a preliminary ...
Xen e OpenVirtuozzo
It Appropriation Request
The Megasite: Infrastructure for Internet Scale
Perl 1997 Paper
Filling The Gap - Going Mobile With JBoss Technologies Today
Css 2010

Similar to Dynamic Web Pages 2009v2.1 (20)

PDF
23617968 digit-fast-track-jan-2009-php
PDF
php_mysql_2006
PDF
php_mysql_2006
PPTX
Week 1
PDF
Jax2010 adobe lcds
PDF
No Really, It's All About You
PDF
Eva flex java_1_slides
PDF
Tips
PDF
Technote Index Map Help
PDF
Technote Index Map Help
PPT
6 3 tier architecture php
PDF
PHP in the Real World
PPT
PHP and MySQL
PDF
Sap to php
PPTX
Website development courses
PDF
Rc023 php online
PPTX
190670107040 summer internshiip ppt.pptx
PPTX
Software development
ODP
Drupal 6 - podstawy - www.openbit.pl
23617968 digit-fast-track-jan-2009-php
php_mysql_2006
php_mysql_2006
Week 1
Jax2010 adobe lcds
No Really, It's All About You
Eva flex java_1_slides
Tips
Technote Index Map Help
Technote Index Map Help
6 3 tier architecture php
PHP in the Real World
PHP and MySQL
Sap to php
Website development courses
Rc023 php online
190670107040 summer internshiip ppt.pptx
Software development
Drupal 6 - podstawy - www.openbit.pl
Ad

More from Cathie101 (20)

PPT
Page Layout 2010
PPT
Xhtml 2010
PPT
Dynamic Web Pages Ch 8 V1.0
PPT
Dynamic Web Pages Ch 6 V1.0
PPT
Dynamic Web Pages Ch 1 V1.0
PPT
Dynamic Web Pages Ch 9 V1.0
PPT
Dynamic Web Pages Ch 7 V1.0
PPT
Dynamic Web Pages Ch 5 V1.0
PPT
Dynamic Web Pages Ch 4 V1.0
PPT
Dynamic Web Pages Ch 3 V1.0
PPT
Dynamic Web Pages Ch 2 V1.0
DOC
Database Fdd
DOC
Sql All Tuts 2009
DOC
Database Fdd
DOC
Database Er
PPT
Database Design E R 2009
PPT
Database Design Fdd 2009
PPTX
Database Design E R 2009
DOC
Overall Computer Systems 2nd Year 2009
PDF
All Database Design Tuts V1.3
Page Layout 2010
Xhtml 2010
Dynamic Web Pages Ch 8 V1.0
Dynamic Web Pages Ch 6 V1.0
Dynamic Web Pages Ch 1 V1.0
Dynamic Web Pages Ch 9 V1.0
Dynamic Web Pages Ch 7 V1.0
Dynamic Web Pages Ch 5 V1.0
Dynamic Web Pages Ch 4 V1.0
Dynamic Web Pages Ch 3 V1.0
Dynamic Web Pages Ch 2 V1.0
Database Fdd
Sql All Tuts 2009
Database Fdd
Database Er
Database Design E R 2009
Database Design Fdd 2009
Database Design E R 2009
Overall Computer Systems 2nd Year 2009
All Database Design Tuts V1.3
Ad

Recently uploaded (20)

PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Modernizing your data center with Dell and AMD
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
cuic standard and advanced reporting.pdf
PDF
Approach and Philosophy of On baking technology
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Advanced IT Governance
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Modernizing your data center with Dell and AMD
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
cuic standard and advanced reporting.pdf
Approach and Philosophy of On baking technology
Unlocking AI with Model Context Protocol (MCP)
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Review of recent advances in non-invasive hemoglobin estimation
The Rise and Fall of 3GPP – Time for a Sabbatical?
Advanced IT Governance
Chapter 3 Spatial Domain Image Processing.pdf
Advanced Soft Computing BINUS July 2025.pdf
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025

Dynamic Web Pages 2009v2.1

  • 1. 28/06/2009 Dynamic Web Pages PHP 1 Authors & Modifications First Edition 2006 Version 1.0 1.1 2.0 2.1 No. Authors Date 07/06 08/06 07/08 07/09 Cathie Usher Contact Cathie Usher Cathie Usher Cathie Usher Cathie Usher 2 Database php version 2.1 1
  • 2. 28/06/2009 Chapter 1 Site management & Sending & Receiving data between pages. 3 Dynamic web page decisions • The first decision is to decide what scripting language you are going to p g g g y g g write your dynamic pages in. – PHP: Generally uses an Apache server with php. – ASP: Generally uses Microsoft Internet Information Ser ces ( S), wh ch has nformat on Services (IIS), which an asp engine incorporated. 4 Database php version 2.1 2
  • 3. 28/06/2009 How PHP Pages are Accessed and Interpreted Your PC WebServer (Internet connected) (Internet connected) 1. Web Browser 2. Se Web Server nd Re ques Please Enter t for PHP Software file A Phone 3. Receive Number request, find file and read it. Submit Erase 4. Execute PHP statements 7. Web Browser 5. Send Web Browser results Phone Query back. back lts Results: nR esu etur That is 6. R John Doe's Phone Number We will use WAMPS (Windows, apache, mysql, php server) and it will be locally installed on the lab machines. 5 WAMPS • This web service has three servers; – Apache p – MYSQL – PHP • Wamps will be located locally on the lab machines. So the lab machines will be local and server machine. b al l ds hi 6 Database php version 2.1 3
  • 4. 28/06/2009 Creating a PHP Script File and Saving It to a Local Disk You can use a number of different editors to create your PHP script files. P P fl – The PHP script starts with a <?php tag and ends with ?>. – Between these tags is a single PHP print statement. 7 Alternative PHP Delimiters • You can alternatively start your PHP scripts with the <script> tag as follows: p p g f <script language="PHP"> print ("A simple initial script"); </script> • If have short_open_tag enabled in its configuration file, you can use <? and ?>. • If asp_tags is enabled in the PHP configuration file, you can use <% and %> as delimiters. 8 Database php version 2.1 4
  • 5. 28/06/2009 A Little About PHP's Syntax • Some PHP Syntax Issues: – Be careful to use quotation marks, parentheses, and brackets in pairs. – Most PHP commands end with a semicolon (;). – Be careful of case. – PHP ignores blank spaces. l 9 Embedding PHP Statements Within HTML Documents • One way to use PHP is to embed PHP scripts within HTML tags in an HTML document. 1. <html> 2. <head> 3. <title>HTML With PHP Embedded</title> </head> 4. <body> 5. <font size=5 color=”blue”>Welcome To My Page</font> 6. <?php 7. print ("<br> Using PHP is not hard<br>"); 8. 8 ?> 9. and you can learn to use it quickly! 10. </body></html> 10 Database php version 2.1 5
  • 6. 28/06/2009 Using Backslash () to Generate HTML Tags with print() • Sometimes you want to output an HTML tag that also requires double quotation h l d bl marks. – Use the backslash (“”) character to signal that the double quotation marks themselves should be output: print ("<font color="blue">"); – The above statement would output: <font color="blue"> 11 Using Comments with PHP cripts • Comments enable you to include y descriptive text along with the PHP script. – Comment lines are ignored when the script runs; they do not slow down the run-time. – Comments have two common uses. • Describe the overall script purpose. • Describe particularly tricky script lines. 12 Database php version 2.1 6
  • 7. 28/06/2009 Using Comments with PHP Scripts • Comment Syntax - Use // y <?php // This is a comment ?> • Can place on Same line as a statement: <?php print ("A simple initial script"); //Output a ( A script ); line ?> 13 Example Script with Comments 1. <html> <head> 2. <title> Generating HTML From PHP</title> </head> g / / 3. <body> <h1> Generating HTML From PHP</h1> 4. <?php 5. // 6. // Example script to output HTML tags 7. // 8. print ("Using PHP has <i>some advantages:</i>"); 9. print ("<ul><li>Speed</li><li>Ease of use</li> <li>Functionality</li></ul>"); //O li i li /li / l ") //Output b ll bullet li list 10. print ("</body></html>"); 11. ?> 14 Database php version 2.1 7
  • 8. 28/06/2009 Alternative Comment Syntax PHP allows a couple of additional ways to create comments. <?php phpinfo(); # This is a built-in function ?> • Multiple line comments. <?php /* A script that gets information about the PHP version being used. */ <? phpinfo(); ?> 15 Using PHP Variables • Variables are used to store and access data in computer memory. p y • A variable name is a label used within a script to refer to the data. Name of variable $cost = 4.25; Variables new value $months = 12; 16 Database php version 2.1 8
  • 9. 28/06/2009 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. 17 Selecting Variable Names • You can select just about any set of characters for a variable name in PHP PHP, but they must: – Use a dollar sign ($) as the first character – Use a letter or an underscore character (_) as the second character. • N Note:Try to select variable names that T l i bl h help describe their function. For example $counter is more descriptive than $c or $ctr. 18 Database php version 2.1 9
  • 10. 28/06/2009 Combining Variables and the print Statement • That is, to print out the value of $x is $x, write the following PHP statement: – print ("$x"); • The following code will output “Bryant is 6 years old”. $age=6; print ("Bryant is $age years old."); 19 A Full Example ... 1. <html> 2. 2 <head> <title>Variable Example </title> </head> 3. <body> 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 <br> second_num=$second_num"); 11. ?> </body> </html> 20 Database php version 2.1 10
  • 11. 28/06/2009 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.” 21 Common PHP Numeric Operators Table 2.1 Common PHP Numeric Operators Operator Effect Example Result + Addition $x = 2 + 2; $x is assigned 4. - Subtraction $y = 3; $y is assigned 2. $y = $y – 1; / Division $y = 14 / 2; $y is assigned 7. * Multiplication $z = 4; $y is assigned 16. $y = $z * 4; % Remainder $y = 14 % 3; $y is assigned 2. 22 Database php version 2.1 11
  • 12. 28/06/2009 A Full Example 1. <html> 2. <head> <title>Variable Example </title> </head> 3. <body> y 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 <br>"); 16. print ("Total Revenue is $total_revenue <br>"); 17. print ("Total Profit is $profit"); 18. ?> </body> </html> 23 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 ("x=$x y=$y"); ?> 24 Database php version 2.1 12
  • 13. 28/06/2009 Writing Complex Expressions • Operator precedence rules define the d in hi h th th order i which the operators are t evaluated. For example, $x = 5 + 2 * 6; • The value of $x is either 42 or 17 depending on order of evaluation. p g f • Since multiplication evaluated before addition operations, this expression evaluates to 17. 25 PHP Precedence Rules • PHP follows the precedence rules listed below. – First it evaluates operators within parentheses. – Next it evaluates multiplication and division di i i operators. t – Finally it evaluates addition and subtraction operators. 26 Database php version 2.1 13
  • 14. 28/06/2009 PHP Precedence Rules • For example, the first 2 statements evaluate to 80 while the last to 180. – $x = 100 - 10 * 2; – $y = 100 - (10 * 2); – $z = (100 - 10) * 2; 27 A Full Example 1. <html> 2. 2 <head> <title>Expression Example </title> </head> 3. <body> 4. <?php 5. $grade1 = 50; 6. $grade2 = 100; 7. $grade3 = 75; 8. $average = ($grade1 + $grade2 + $grade3) / 3; 9. print ("The average is $average"); 10. ?> </body> </html> 28 Database php version 2.1 14
  • 15. 28/06/2009 Working with PHP String Variables • Character strings are used in scripts to hold data such as customer names names, addresses, product names, and descriptions. • Consider the following example. – $name="Christopher"; – $preference="Milk Shake"; • $name is assigned “Christopher” and the variable $preference is assigned “Milk Shake”. 29 WARNING: Be Careful Not to Mix Variable Types • Be careful not to mix string and numeric variable types. • For example, you might expect the following statements to generate an error message, but they will not. Instead, they will output “y=1”. <?php $x ="banana"; $sum = 1 + $x; print ("y=$sum"); ?> 30 Database php version 2.1 15
  • 16. 28/06/2009 Using the Concatenate Operator • The concatenate operator combines two separate string variables i t one. t t i i bl into • For example, – $fullname = $firstname . $lastname; • $fullname will receive the string values of $firstname and $lastname connected together. g • For example, $firstname = "John"; $lastname = "Smith"; $fullname = $firstname . $lastname; print ("Fullname=$fullname"); 31 TIP; An Easier Way to Concatenate Strings • You can also use double quotation marks to q create • concatenation directly, • For example, • $Fullname2 = "$FirstName $LastName"; • Thi statement h This t t t has th same effect as the ff t • $Fullname2 = $FirstName . " " . $LastName; 32 Database php version 2.1 16
  • 17. 28/06/2009 The strlen() Function • Most string functions require you to send them one or more arguments. th t • 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 arguments. example $len = strlen($name); Receives the number of Variable or value to work with characters in $name Name of function 33 The strlen() Function Example <?php p p $comments = "Good Job"; $len = strlen($comments); print ("Length=$len"); ?> This PHP script would output “Length=8”. p p g 34 Database php version 2.1 17
  • 18. 28/06/2009 The trim() Function • This function removes any blank characters y from the beginning and end of a string. For example, consider the following script: – <?php – $in_name = " Joe Jackson "; – $name = trim($in_name); – print ("name=$name$name"); – ?> 35 The strtolower() and strtoupper() Functions • These functions return the input string in all uppercase or all l pp s ll lowercase l tt s respectively. s letters, sp ti l • For example, <?php $inquote = "Now Is The Time"; $lower = strtolower($inquote); $upper = strtoupper($inquote); print ("upper=$upper lower=$lower"); ?> • The above would output “upper=NOW IS THE TIME lower=now is the time”. 36 Database php version 2.1 18
  • 19. 28/06/2009 The substr() Function – Substr has the following general format: Assign the Starting position to extracted sub- start extraction from. string into this $part = substr( $name, 0, 5); variable. Number of characters to extract. (If omitted it will Extract from this continue to extract until the end string variable variable. of the string.) f th t i ) 37 The substr() Function • The substr() function enumerates character positions starting with 0 (not 1) 1), – 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. • For example, the following would output “Month=12 Day=25”. <?php $date = "12/25/2002"; $month = substr($date, 0, 2); $day = substr($date, 3, 2); print ("Month=$month Day=$day"); ?> 38 Database php version 2.1 19
  • 20. 28/06/2009 The substr() Function • As another example, consider the following use of the substr() function – It does not include the third argument (and thus returns a substring from the starting position to the end of the search string). <?php $date = "12/25/2002"; $year = substr($date, 6); print ("Year=$year"); ?> • The above script segment would output “Year=2002”. 39 Servers and files • The php files need to be located on the server. • The location for wamps is C:wampswww • All files must be place here. • In dreamweaver you will work on files that are local on your storage space: f, h etc. • When these local files are save they will automatically be uploaded to the server. 40 Database php version 2.1 20
  • 21. 28/06/2009 Dynamic Web pages requirements • Create a local site: shown on the next slide. • Check WAMPS is running. – On the lab machines sometimes the IIS starts first and blocks the port. If this happens you must stop the IIS. • Create a dynamic and testing site: shown on the following slides. 41 Creating a local site Local folder is your y H or F drive etc. 42 Database php version 2.1 21
  • 22. 28/06/2009 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. 43 Creating a Testing Server 44 Database php version 2.1 22
  • 23. 28/06/2009 Transferring to the Remote server • Click on the “expand show local & remote sites” button. • Click on the “put files” button; the blue arrow. 45 Changing htm to php • Change all the htm & html pages to php. • This is done by click on the name in the file panel and changing the extension. (same as renaming in windows). • An update screen will appear. appear • Click ok. 46 Database php version 2.1 23
  • 24. 28/06/2009 Check the pages • Open each page one at a time followed by the “f12” key to execute y y the page in the browser. • Once all pages have been executed you should be able to test the links on each page. • If th li k are not working, check the links t ki h k they are to the asp extension pages. 47 Using forms to send data. • Overview: – In this section you will create pages with embedded forms. ith b dd d f – The forms will have objects, such as text fields, submit buttons, radio buttons etc. – The data in the form objects will be sent to the server and be displayed on another page. 48 Database php version 2.1 24
  • 25. 28/06/2009 Retrieving data • Overview: – The are four ways of transferring data y g from one page to another. • Through a form with the post method. • Through a form with the get method. • Through a URL link • Through a cookie. 49 Creating HTML Input Forms • HTML Forms are not part of PHP language but important way to send data to scripts Text Box Radio Buttons Check Box Select Box Text Area Submit/Reset button 50 Database php version 2.1 25
  • 26. 28/06/2009 Starting And Ending HTML Forms • You can create HTML forms by using the HTML <form> and </form> tags tags. Program to start when form is submitted. <form action="http://webwizard.aw.com/~phppgm/program.php" method="post"> Format to send data. . . Place form elements between <form> and </form> tags. </form> . Forms end with </form> 51 Creating Form Buttons • You can create submit and reset buttons by placing the following within <form> & </form> tags. <input type=”submit” value=”Click To Submit”> <input type=”reset” value=”Erase and Restart”> Type of button to create Button Label • The submit button will be labeled “Click To Submit”. The reset button will be labeled “Erase and Restart”. 52 Database php version 2.1 26
  • 27. 28/06/2009 Another Full Script Example 1.<html> 2.<head> <title> A Simple Form </title> </head> 3.<body> 3 <body> 4.<form action="http://webwizard.aw.com/~phppgm/First.p hp" method="post" > 5. Click submit to start our initial PHP program. 6. <br> <input type="submit" value="Click To p yp Submit"> 7. <input type="reset" value="Erase and Restart"> 8. </form> 9. </body> </html> 53 Receiving Form Input into PHP Scripts • To receive HTML form input into a PHP script: – Use a PHP var name that matches the variable defined in the form element’s name argument. • For example, if form uses the following: – <input type="radio" name="contact" value="Yes"> • Then form-handling PHP script could use a variable called $ $contact. – If the user clicks the radio button, then $contact would = Yes 54 Database php version 2.1 27
  • 28. 28/06/2009 Full Example • Suppose your HTML form uses the following: – Enter email address: <input type="text" size="16" maxlength="20" name="email"> g • Then can receive input as follows: 1.<html> 2. <head><title> Receiving Input </title> </head> 3. <body> 4. <font size=5>Thank You: Got Your Input.</font> 5. 5 <?php 6. print ("<br>Your email address is $email"); 7. 8. print ("<br> Contact preference is $contact"); 9. ?> 55 Register_Globals? • Since PHP 4.2.1, the default PHP configuration is requiring a different mechanism t receive i h i to i input f security t for it reasons (than the one just shown). – Technical details: it is a PHP configuration option to turn REGISTER_GLOBALS OFF (new default) or ON in the php.ini configuration file. • If your site has REGISTER_GLOBALS OFF you must use a different mechanism to receive HTML Form Variables. 56 Database php version 2.1 28
  • 29. 28/06/2009 How can you tell if Register_Globals is OFF? • Enter the following PHP script and run it. g p – <?PHP phpinfo(); ?> • Search through the output for REGISTER_GLOBALS and see if it is set to OFF or ON. • If it i off you must use th f ll i way t is ff t the following to receive input data. 57 Getting input data with Register_Globals OFF? • To receive data with REGISTER_GOBALS OFF you use a special variable called $POST. FF l bl ll d $P Enclose in square – $name $_POST[“name”]; bracket and then quotes Name of HTML form variable (note do not use $) i bl ( t d t Special PHP Global variable. Technically it is an associative array (covered in chptr 5.) PHP variable name that you want to receive 58 the HTML form input. Database php version 2.1 29
  • 30. 28/06/2009 Full Example, when REGISTER_GLOBALS is 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> y 4. <font size=5>Thank You: Got Your Input.</font> 5. <?php 6. $email = $_POST[“email”]; 7. $contact = $_POST[“contact”]; 8. print ("<br>Your email address is $email"); 9. print ("<br> Contact preference is $contact"); 59 10. ?> Forms sending & retrieving 1 • Create a new page and save as test_form.php. • Title : Using a form. • Add a form, 2 textfield and a submit button. • The page should look like; 60 Database php version 2.1 30
  • 31. 28/06/2009 Forms sending & retrieving 2 • Properties of objects • Textfield 1: – N Name= firstName fi tN • Textfield 2: – Name=lastName • Form – Name=frmName – Action=test form processor php Action=test_form_processor.php – method-=POST. 61 Forms sending & retrieving 3 • Create another page called p p p test_form_processor.php. • Title: Displaying form data. • Create the page as shown and select the plus to bind the form data. 62 Database php version 2.1 31
  • 32. 28/06/2009 Forms sending & retrieving 4 • Select the form variable. • Select the Request.Form type. • The name should be the same as the name on the page the data is coming from. f In this case the data is coming from test_form.php 63 Forms sending & retrieving 5 • Place the cursor after the comer after the “Thank you ”. • Click on the binding firstName and then click on the insert button. This inserts the request variable into that space on the page. • To insert the binding you can also drag and drop as another method. d th th d • Repeat this for the lastName variable. 64 Database php version 2.1 32
  • 33. 28/06/2009 Forms sending & retrieving 6 • Your page should look like this. • Now execute the test form php test_form.php page. Note: You will need to put a non breaking space (&nbsp;) between the two form bindings 65 Looking at the code. • Normally php code is embedded with <?php ?> • Echo is the command to display information. There are two other commands: print() and printf(). • The two print command allow formatting of text. 66 Database php version 2.1 33
  • 34. 28/06/2009 Retrieving data via URL 1 • Open test_form.php and save as test_form_get.php. • Change the form method to GET GET. • Open test_form_processor.php file and save as test_form_processor_get.php. • Delete the bindings. • Insert 2 new bindings: – the URLVariable URLVariable. – Next slide shows how. 67 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 g will appear. Drag it to where the variable should be shown. The next slide shows this. 68 Database php version 2.1 34
  • 35. 28/06/2009 Looking at the code • This code is similar to the form request, except the form has change q p g to GET. 69 Sending data with Hyperlinks. • When you send data with forms and a method of GET, information is sent in the URL. URL • Another method of sending the info in the URL is in a Hyperlink. • To do this you can just add the information in a name and info pair to the url of the link. eg: l f th li k – ?firstName=Larry 70 Database php version 2.1 35
  • 36. 28/06/2009 Sending data with Hyperlinks 2. • 1st page. • Click on a link, eg cat and the page appears like. • Click on dog and the page appears like. 71 Sending data with Hyperlinks 3 • Create a page and save it as animal_question.php and create it to look like thi l k lik this. • The links are to the page animal_home_page.php 72 Database php version 2.1 36
  • 37. 28/06/2009 Sending data with Hyperlinks 4 • The links on the animal_question.php p g page should look like.. • Cat • Dog g 73 Sending data with Hyperlinks 5 • Another way to set the link with p parameters is, click on the browse icon select the file and click on the parameters buttons as shown. 74 Database php version 2.1 37
  • 38. 28/06/2009 Sending data with Hyperlinks 6 • The animal_home_page.php should look like. • Create the URL Variable as before with the binding panels. 75 Cookies - 1 • The form and querystring request are stateless variables. Once the server has passed th d them onto th client the server has t the li t th h no memory of them. • Cookies are tiny text files that are written onto the user’s hard drive. • Cookies help to maintain state over coming the t t l th stateless HTTP protocol. t l 76 Database php version 2.1 38
  • 39. 28/06/2009 Cookies - 2 • Cookies and security – Cookies can’t be used to infect computer p with a virus. – Cookies can only be read by the site that sent them. – Cookies should never contain credit card details, password, etc. ,p , 77 Cookies - 3 • Open the test_form_processor_get.php file . • Create a new page test_form_processor_cookies.php • Add the following to the test_form_processor_get.php file. file Cookie variable name This code must be place before the html tag 78 Database php version 2.1 39
  • 40. 28/06/2009 Cookies - 4 • Create the bindings and place them in the design view of the g test_form_processor_cookies.php file. • As shown, then save and preview. 79 Lab 1 • Objectives • After completing this lab, you should p g y be able to: – Create URL variables in the Bindings panel. – Display URL variables on a page. – Create FORM variables in the Bindings panel. l – Display FORM variables on a page. 80 Database php version 2.1 40
  • 41. 28/06/2009 Lab 1 cont-2 • Copy the folder lab_start from the x drive to your working drive. y g • Create a new site called lab • Setup the folders on the IIS root folder and the remote and testing servers. 81 82 unit2sendingpage.php Database php version 2.1 41
  • 42. 28/06/2009 unit2formactionpage.php 83 unit2urlactionpage.php?firstname=Larry 84 Database php version 2.1 42
  • 43. 28/06/2009 Chapter 2 Sending form details via emails 85 PHP Sending E-mails • PHP allows you to send e-mails directly from a script. y p • The PHP mail() Function • The PHP mail() function is used to send emails from inside a script. 86 Database php version 2.1 43
  • 44. 28/06/2009 Syntax Note: For th N t F the mail f il functions t b available, PHP requires an ti to be il bl i installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. Read more in our PHP Mail reference. 87 PHP Simple E-Mail • This can be used to check if the server is setup correctly. p y 88 Database php version 2.1 44
  • 45. 28/06/2009 Sending Emails - 1 • 1st check that SMTP is installed. Through the window component g p wizard. Highlight IIS and click on details. • If the SMTP is ticked then it is installed if not you need to install it. 89 Sending Emails - 2 • Overview: – In this task you will create a message y g sent page that will transfer the data to an email. – The page that obtains the email data is the contact.php page. You will add a table with no borders and a form to send the information to the message_sent.php page. 90 Database php version 2.1 45
  • 46. 28/06/2009 Sending Emails - 3 • Open the generic_template.php and save it as message_sent.php g p p • Looks like this • Make it look like this, with link to index.php. 91 Sending Emails – 4 • Add the php code as shown. 92 Database php version 2.1 46
  • 47. 28/06/2009 Sending Emails - 5 • Open contact.php and change it to look like this. Details on the next slide. 93 Sending Emails - 6 • Position the insertion point below the image caption, and choose Insert > Form > Form to create a new form. • Without moving the insertion point, choose Insert > Table, providing the following settings. Click OK. – Rows: 4 – Columns: 2 – Width: 95 Percent – Border: 0 – Cell Padding: 3 – Cell Spacing: 0 94 Database php version 2.1 47
  • 48. 28/06/2009 Sending Emails - 7 • Select the <table> tag using the tag selector, and use the Property inspector to change its bgcolor (background color) attribute to #eeeeee, which is a light gray gray. • In the right column of the table insert the following; – Two text fields called, • emailAddress • subject – One text area called body – A Submit button • In the first three cells of the left column, enter the following text: – Your Email Address – Subject – Message Body 95 Sending Emails - 8 • Select the text area and give it the following specifications; – Char Width :55 55 – Num Lines as 6, and Wrap as Virtual. • Select the form in the tag selector, in the Property inspector set: – Name = frm_message – Action = messageSent.php – Method = POST • Insert a Heading 2 element above the table that reads, “Send Us a Message.” 96 Database php version 2.1 48
  • 49. 28/06/2009 Client-Side Form Validation • Open contact.php • Select the submit subm t button • In the behaviours panels add a behaviour as shown. h 97 Client-Side Form Validation • In the Validate form dialog as shown set the fields as – emailAddress: Required as an email – Subject: Required as anything – Body: Required as anything. • Resulting with the dialog shown below. b l 98 Database php version 2.1 49
  • 50. 28/06/2009 Client-Side Form Validation • Notice the behaviours p panel should look like this when you highlight the submit button. • Save and test the validation. validation 99 Lab 2 - 1 • Overview: – Create a html form with client-side validation – Create an action page to send an email. • Your overview instructions located in the next section of the book: lab section. section • Your two pages will look like the following 2 slides. 100 Database php version 2.1 50
  • 51. 28/06/2009 Sendemail.php 101 Sendemail_action.php 102 Database php version 2.1 51
  • 52. 28/06/2009 Sample of email sent 103 Lab 2 - 1 • Open server_side_scripting site • Open tourtemplate.php. • Change to Split view view. • In the Design view pane, click on the link Or email us. • In the Property inspector change the link to link to sendemail.php. Hit the enter key after putting in the link (if you don’t hit enter, enter the value will not be saved). saved) • Save tourtemplate.php. 104 Database php version 2.1 52
  • 53. 28/06/2009 Lab 2 - 2 • Create an Email Form Page – Save tourtemplate.php as sendemail.php. – Replace the generic left heading with Send Email. – Replace the generic Left Side Text with the text “Fill in the values in the form and submit the form to send an email to us. Thank you for your interest.” – Create a new form below the text. • Name the form email. • Set the action to sendemail_action.php. • Make sure that the Method is Post. 105 Lab 2 - 3 • Create a table inside the form with 2 columns and 4 rows. – Type in the labels as shown in the table below. • From Address • Subject j • Message Body – In the second column, • first row put a text field and name it from • With a width of 30 and max of 50. – In the second row, • put a text field and name it subject • with the same width and max. – In the third row, • put a textarea control and name it message p g • with a width of 40 and 10 rows. – In the last row, – first column put a submit button control – with a label of Send Email. 106 Database php version 2.1 53
  • 54. 28/06/2009 Lab 2 – 4 • Create Action Page – Create a new page from tourtemplate.php • name : sendemail_action.php. p p – Note that the email link now points to sendemail.php. • Change the left section title to Email Complete and the text to “Thank you for your email.” • In bindings, create 3 new Request variables: – from – subject – message 107 Lab 2 - 5 • In Code view make space between the heading and the text Type text. the following php code. 108 Database php version 2.1 54
  • 55. 28/06/2009 Lab 2 - 6 • Replace your email address with mine. The From address would normally be y hard coded with the site’s email address. • Save the page. • Preview the sendemail.php page. Fill out the form and submit it using your t th f d b it i email address as the sender. 109 Lab 2 - 7 • Client Side Validation – Return to Dreamweaver and the sendemail.php page. – Select the Submit button. – In the Behaviours panel, click Add Behaviour and choose Validate Form. – Make all of the fields are required and validate the email address. • Save the page. • Preview sendemail.php. • Check that validation is working by trying to submit an empty form and also try an invalid email address format. 110 Database php version 2.1 55
  • 56. 28/06/2009 Chapter 3 Self- Self-server application & Server- Server-side validation 111 Self-server Applications • These applications are answering a q question that would usually be y answered by the staff in the tours company. • An example is how much does it cost to do this tour and another tour. • W will create a tours calculator, We ill t t l l t (self-server application). 112 Database php version 2.1 56
  • 57. 28/06/2009 The tour calculator page - 1 • Open the generic_template.php and save as tourprice_processor.php and p p p p tourprice.php. • The two pages are shown on the next slides. 113 The tour calculator page - 2 Objects names: txtNumberAdults txtNumberChildren cboTourName tourprice.php 114 Database php version 2.1 57
  • 58. 28/06/2009 The tour calculator page - 3 Calculate link to Contact link to contact.php tourprice.php page tourprice_processor.php 115 The tour calculator page - 4 116 Database php version 2.1 58
  • 59. 28/06/2009 The tour calculator page - 5 • tourprice.php has the following properties. – Form • Name = frm_tourprice frm tourprice • Action = tourprice_processor.php • Method = POST – Table inside the form • 4 rows • 2 columns • width of 60 percent – List values • Highlights of Argentina, 500 • Highlights of Western Canada, 700 • Egyptian Pyramids and More, 900 117 Calculating the price - 1 • Open tourprice_processor.php page. • Input the php code as shown to; – declare and initialise the number of adults, kids and tour name. – Calculate the price of the tour. Variable declaration Check you have the same variable names in your tourprice.php page as those in quotes. 118 Database php version 2.1 59
  • 60. 28/06/2009 Calculating the price - 2 • Now you have calculated the tourprice variable, you need to display it. This is done by placing php script amongst the html code as code, shown below. • Save and view the page. What happens when you don’t put in an amount for the adults or kids? 119 The calculator so far…. • So far the calculator works if numeric values are put in the adults p and kids fields. • Errors appear if not values are placed in the adults and kids fields. • The next couple of slides are going to add a css rule f errors and trap dd l for dt the error with php code. 120 Database php version 2.1 60
  • 61. 28/06/2009 Adding new style to the css • Add an error style to the css file with the following properties; – Type: class – Name: error – Weight : bold – Colour : #990000 • This style will be used if the client makes an error with the information for the tour ith th i f ti f th t calculator. 121 Server-side validation -1 • Server-side validation is validating the tourprice.php page variables sent to the tourprice_processor.php page. • This is done by checking the two form variables for numerical values, as shown. • If one of them doesn’t hold a numeric value the client is sent back to the tourprice.php page, displaying an error. • Add the code code. 122 Database php version 2.1 61
  • 62. 28/06/2009 Server-side validation- 2 • Once the client is sent back to the tourprice.php page the code to pick up this error is needed needed. • This code shows the response to that variable and displays a message to the client. 123 Lab 3 - 1 • In this lab you are to add a list/menu to reservations.php and change the form action to call the page reservations_action.php. p g p p • The two pages are shown on the next slides. • To have “Yes” or “No” displayed on equipment you will have to write an if statement finding out if the box was ticked, “true” or not ticked, “false”. If request.form(“equipment”)=“true” then response.write(“No”) (“ ) Else response.write(“Yes”) End If 124 Database php version 2.1 62
  • 63. 28/06/2009 Lab 3 - 2 The value for each label is: 1 for Nepal Track, 2 for Alasken Wilderness, etc. reservations.ph 125 p Lab 3 - 3 Reservation Information from reservation.php page reservations_action.php. This page was created 126 form tourtemplate.php. Database php version 2.1 63
  • 64. 28/06/2009 Chapter 4 Introduction to Databases in PHP 127 Overview • In this chapter you will : – Create a database connection in dreamweaver. – Create a recordset. – Display data from the recordset. 128 Database php version 2.1 64
  • 65. 28/06/2009 Placing the database on the server • We are going to using the database y you created in the sql component: q p newlands. • Make sure this is database is in the sql directory as shown below. 129 Exploring the Database • Open the database called newland_tours.mdb. • This is the database that you will be working with. • The relationships look like this. 130 Database php version 2.1 65
  • 66. 28/06/2009 Connecting the site to a database • Select the + as shown. • The following sql follow ng connection appears, Note the select it. database tag has • This will produce a dialog been selected. as shown. Complete the dialog as shown. Test the connection and click OK 131 Check the Connection • Click on the + and explore the tables etc. • Note a folder has been added to your site with the connection php file. • This file is a Dreamweaver configuration. 132 Database php version 2.1 66
  • 67. 28/06/2009 What’s in the connection file? 133 Displaying data Using a recordset- 1 • Open the index.php file p p in design view and delete the highlight text. 134 Database php version 2.1 67
  • 68. 28/06/2009 Displaying data - 2 • Select the traveller’s journal image and right click on the h3 tag in the tag selector. • Select the remove tag from the pop up menu. 135 Displaying data - 3 • Select Creating a recordset the + in the th bindings as shown. • Fill in the dialog as shown. 136 Database php version 2.1 68
  • 69. 28/06/2009 Displaying data - 4 • Drag and drop description as shown. shown • Looks like • You can also view Data using the live data as shown 137 Lab 4 -1 • Overview – Create sql connection for the lab q database – Create a dreamweaver connection – Create a recordset for the equipment. – Create a dynamic table using the equipment recordset recordset. 138 Database php version 2.1 69
  • 70. 28/06/2009 Lab 4 equpment.php Design Dynamic table 139 Lab 4 equipment.php live 140 Database php version 2.1 70
  • 71. 28/06/2009 Lab 4 -2 • Connect the site to the database – call it dwda. • Open equipment.php page. • Create a recordset – Call it rs_equip – Select all field except EquipID – Sort on EquipType field in Ascending order. • Test the recordset. 141 Lab 4- recordset 142 Database php version 2.1 71
  • 72. 28/06/2009 Lab 4 -3 • Select the HTML table containing equipment and delete the table. • On the Application tab on the Insert bar, click Dynamic Data and choose Dynamic Table. • Choose all records from the rs_equip recordset and give a border of 1 and a cellpadding of 3. • Click OK. • Change the headings to be more descriptive and bold them. • Save the page and preview it. 143 Lab 4 -4 • Format the price column – Select the binding on the page for the price column. – In the Bindings panel, the price column will be g p , p highlighted. Drop down the Format column’s select box and choose Currency > Default. Refer to next slide for menus. • Note: You may not see the Format column in the Bindings panel if it is too narrow. You can expand the panels or scroll within the Bindings panel. l • Preview the page. You should see the same list of equipment now coming from the database. 144 Database php version 2.1 72
  • 73. 28/06/2009 Formatting numbers. 145 Chapter 5 Dynamical Calculations 146 Database php version 2.1 73
  • 74. 28/06/2009 Overview • In this chapter you will: – Dynamically populate a drop-down menu. y yp p p – Create a recordset using a filter from the form data on the previous asp page. – Insert dynamic text. – Write the code to handle errors when data has not validated validated. 147 Dynamically populated drop- down menus. The next activity will create a drop-down d d menu which is populated from the database through a recordset. tourprice.php 148 Database php version 2.1 74
  • 75. 28/06/2009 Dynamic calculation • Remember the Tour Calculator. • We are going to change it to dynamically put the tour locations in the list/menu, (with the ID being the value) on the tourprice.php page. • On the tourprice_processor.php, we will then use the id to select the adult and child rate t calculate th cost or the hild t to l l t the t th tour. 149 Create a tourprice recordset Test the recordset View the sql statement with the advanced button. 150 Database php version 2.1 75
  • 76. 28/06/2009 Dynamic drop-down • Open tourprice.php page and select the list/menu. • Select the dynamic button. • Remove the static labels and values with the – button. 151 Dynamic drop-down-2 • Fill the dialog as g shown. 152 Database php version 2.1 76
  • 77. 28/06/2009 Dynamic drop-down-3 • Save and view as shown. 153 Next Activity: Creating dynamic text Dynamic text tourprice_processor.php Click on the submit button button. The data in the form is transferred to the tourprice.php tourprice_processor.php page. The price is calculated using the form data. 154 Database php version 2.1 77
  • 78. 28/06/2009 Dynamic calculation • Open tourprice_processor.php page. • Create a new recordset (shown on next slide), with the following attributes: – Name = rsTourpricesFiltered – Connection = conn_newland – Table = tours – Columns = selected tourID, tourName, basePriceAdult, basePriceChild 155 Filtered recordset • Click the Test button. Enter 9 in the Please P id th Pl Provide a Test Value dialog, and click OK. • Click OK to exit the test output output, and again to save the recordset. Note: This recordset has a filter on it. The recordset will only select a tour id that equals tour is that was selected from the pervious asp page. 156 Database php version 2.1 78
  • 79. 28/06/2009 Displaying dynamic text • Place xx as shown. • Highlight xx. g g • Select Dynamic Text as shown. • The dialog shown appears, select tourName and click OK. 157 Fixing up the price. • Delete the code that sets the base p price. • Add the following lines 158 Database php version 2.1 79
  • 80. 28/06/2009 Avoiding errors • Add the following to avoid calculation errors. • Save and view the pages. 159 Lab 5 - 1 • In this lab you will; – Create a dynamic list/menu for the y reservation.php page, shown on the next slide. – Add a dynamic feature tour list, as shown on the following slides. 160 Database php version 2.1 80
  • 81. 28/06/2009 Lab 5 - 2 Dynamic tour list •Note: Duplicate entries because the same trip is Reservation.php offered on different dates. 161 Lab 5 – 3 tourtemplate.php Static Featured tours Dynamic Feature tours 162 Database php version 2.1 81
  • 82. 28/06/2009 Lab 5 - 4 • reservations.php page. • create a recordset – named rs_tours from th tours table. nam rs tours the ta . – Select only TourID, TourName, and TourDate – sort by TourName. • Click the Dynamic button on the Property inspector. – Dynamically fill the values from the recordset that you just created. The values are the TourID created – Labels are the TourName – Click OK to close the dynamic dialog. • Save the page and preview it. Drop down the list to see the dynamic values. 163 Lab 5 - 5 • Create a recordset • Delete the text under the Featured Tours heading. • Add a dynamic table as shown. Remember border=0. Save and view. 164 Database php version 2.1 82
  • 83. 28/06/2009 Chapter 6 Filtering & Displaying Data 165 Overview • In this chapter you will: – Transfer dynamic data through the URL. y g – Add repeating regions. – Creating recordsets with information from the URL. – Adding images dynamically. 166 Database php version 2.1 83
  • 84. 28/06/2009 Changing profiles.php • The profiles page displays the countries statically. • We are going to change this to dynamically from the database. 167 Profiles current page Profiles_details.php • Create a profiles_details.php page from the generic template. g p 168 Database php version 2.1 84
  • 85. 28/06/2009 profiles.php - 1 • Delete the countries so the p profiles.php page is as shown. p pp g 169 profiles.php - 2 • Create a recordset as shown. 170 Database php version 2.1 85
  • 86. 28/06/2009 profiles.php - 3 • Insert a country name field binding, as shown. • With this insertion highlighted click on the browse icon for a link. • Filling the browse as shown. • Select the parameters button. 171 profiles.php – 4 • In the parameters dialog box, click under the name column heading and type the names as shown. • Click on the lighting bolt and select as shown for both values. • This parameters button writes the url code in the link. li k • Here we are passing the country ID and name in the URL. 172 Database php version 2.1 86
  • 87. 28/06/2009 profiles.php – 5 • Notice now the binding is a link. • Notice the link sends the id of the country in the URL. • With the link highlighted – In the server behaviours Panel click + and choose Repeat Region, as shown. 173 profiles.php – 6 • Adding a repeat region and click on OK. • A section page of the page is shown. Check that the break is inside the repeat region 174 Database php version 2.1 87
  • 88. 28/06/2009 profiles.php – 5 • View the page. It should look like 175 profiles_details.php • Open the profiles_details.php • Create a recordset and complete the page to look like below. • The following slide have the details on how complete it. Heading 1 Heading 2 process_details.php 176 Database php version 2.1 88
  • 89. 28/06/2009 Create the recordest Click the advanced.. button. 177 Combining two tables • Add the other table; region. • Add the where clause to join the tables. t bl 178 Database php version 2.1 89
  • 90. 28/06/2009 Add Bindings & View • Add the bindings as shown on the pervious slides. • The finished page should look like: 179 Inspecting the pervious sql • Take a look at the Sql • What does it mean t 180 Database php version 2.1 90
  • 91. 28/06/2009 Dynamic images -1 • Now we are going to add the image that go with the country. g y • Place the cursor as shown. • From the menu select insert->image. 181 Dynamic images -2 • Select as shown and click OK li k OK. • Create the imageright css rule and apply it to the image. 182 Database php version 2.1 91
  • 92. 28/06/2009 Dynamic images -4 • Completed page. 183 Lab 6 -1 • Dynamically creating a repeated region for the tour packages and their dates. • Then the tour ID is sent in the URL to open the tourpackages.php page. • The tourpackage.php page uses the tour ID to display the info about the selected tour package. • The following slides show the changes of the two pages. 184 Database php version 2.1 92
  • 93. 28/06/2009 Lab 6 -2 • What the page look like now. tours.php 185 Lab 6 -3 • What the page will look like at the end tours.php 186 Database php version 2.1 93
  • 94. 28/06/2009 Lab 6 -4 • Current page tourpackages.php 187 Lab 6 -5 • What the page will look like at the end tourpackages.php 188 Database php version 2.1 94
  • 95. 28/06/2009 Chapter 7 SQL with Joins & Recordset Paging 189 Overview • In this chapter you will: – Use the SQL term INNERJOIN. Q – Adding a recordset navigational tool bar. 190 Database php version 2.1 95
  • 96. 28/06/2009 tours_detail.php - 1 • For this activity you are going to; – create a page that joins the information p g j from two tables on a set of criteria. – Add a recordset paging object. • The page is shown on the next two slides. 191 Part 1 of tours_details.php 192 Database php version 2.1 96
  • 97. 28/06/2009 Part 2 of tours_details.php • Adding recordset Paging 193 tours_detail.php - 2 • Open generic_template.php – Title: Newland Tours: Tour Descriptions. p – Save as tour_details.php. • Make the following changes to the page as shown. Heading 2 g Link to tours.php 194 Database php version 2.1 97
  • 98. 28/06/2009 tours_detail.php - 3 • Create a recordset as follows; 195 tours_detail.php - 4 Dynamic image Table width=60% For these links refer to following slides. Using the recordset add the bindings and the repeat region ( 5 records at a time), to the page as shown. 196 Database php version 2.1 98
  • 99. 28/06/2009 tours_detail.php - 5 • The link “Learn more about” has the following details. 197 tours_detail.php - 6 • Price this tour with Tour price calculator. 198 Database php version 2.1 99
  • 100. 28/06/2009 tours_detail.php - 7 • Insert the dynamic image’s placeholder and set it right align. 199 Adding a repeat region • Highlight all the recordset data. • Select the server behav our tab. behaviour • Click on the plus symbol and select repeat region. • Select five records at a time, as shown below. 200 Database php version 2.1 100
  • 101. 28/06/2009 Part 2 Recordset Navigation-1 • Inserting a recordset navigation can be done by the following two. 201 Recordset Navigation-2 • When you add a navigation the recordset dialog appears as shown. g pp • Save and preview. p 202 Database php version 2.1 101
  • 102. 28/06/2009 Fixing up the tourprice.php page • Create a querystring variable as shown below. • Click to select the drop-down menu in the form, beside Tour name. In the Property inspector, click the Dynamic button. Shown on the next slide. 203 Dynamic list Click on the lighting bolt and the Dynamic Data dialog box appears. 204 Database php version 2.1 102
  • 103. 28/06/2009 Lab 7 -1 • This lab creates a page called bookedtours.php. p p • The tour bookings information is shown on the next slide. 205 Lab 7 -2 206 Database php version 2.1 103
  • 104. 28/06/2009 Chapter 8 Dynamic searching 207 Creating a search. • In this chapter you are going to create sql statements that search q for information. • The next slides show the changes to the tours.php page, tours_details.php page and index.php page 208 Database php version 2.1 104
  • 105. 28/06/2009 tours.php 209 tours_details.php 210 Database php version 2.1 105
  • 106. 28/06/2009 index.php 211 Preparing the search page. • Make the changes to the tours.php p g page as shown on the next page. p g 212 Database php version 2.1 106
  • 107. 28/06/2009 tours.php page 1.Change text Link to tourprice.php 2. Insert table Recordset for the dynamic list values values. A recordset A form with the shown on following details next page. 213 Creating the recordsets -1. 214 Database php version 2.1 107
  • 108. 28/06/2009 Creating the recordsets -2. 215 Dynamic list 216 Database php version 2.1 108
  • 109. 28/06/2009 Creating the repeating section 217 Region Link The filtering on the link would work until we change the query on the tour_details.php page which is done later. 218 Database php version 2.1 109
  • 110. 28/06/2009 Now fix the index.php Profiles.php link Contact.php link. A recordset and a repeat region. This is the same as the world region recordset in the tours.php page. 219 h1 tours_details.php page h2 Image shown on the following pages Recordset shown on the following slides Insert a Link to Li k t table toursprice.php shown on the next page 220 Database php version 2.1 110
  • 111. 28/06/2009 Tour price calculator link 221 Tour details recordset 222 Database php version 2.1 111
  • 112. 28/06/2009 Image details 223 Adding a recordset navigation • Add a recordset navigation to the bottom of the tours_details.php page as shown shown. • Add the find Tours link to tours.php page 224 Database php version 2.1 112
  • 113. 28/06/2009 Looking at the code Add this code to tours_details.php When you select view all countries from the tours.php page this is the recordset you what to use. When you click on If you select a country the region link from the tours.php page this is the then this is the recordset to use recordset to use. 225 Adding an if in case there are no records Add these lines 226 Database php version 2.1 113
  • 114. 28/06/2009 Lab 8 Overview • In this lab you will: – Create a search interface by creating a form and allowing the user to select a value from a list/menu object and returning the tours that match the selected value. • Objectives: – Group data in a select statement – Create a search page from a form – Implement a search results page using the value the user selected on the form. – Add conditional logic to the SQL statement. 227 You will create this page Searchreservations.php 228 Database php version 2.1 114
  • 115. 28/06/2009 Receiving data from searchreservations.php bookedtours,.php 229 Chapter 9 Using Session variables, hidden fields & inserting data. Database php version 2.1 115
  • 116. 28/06/2009 Overview • In this chapter you will – Create restricted administration pages. p g – Use Session variables – Use hidden fields – Insert records into a table. Creating an Insert Journal Pg -1 Open the generic_template.php. Save as journalAdd.php. Change the page as shown above. Database php version 2.1 116
  • 117. 28/06/2009 Insert a record • On the journalAdd.php j lAdd h page and select the “Insert Record” as shown. • The next slide he sl de shows the screen that appears. Inserting a record • Notice that the JournalID field needs a value. • This value needs to be increased by one. – Eg: last value is 3 the next value needs to be 4. Database php version 2.1 117
  • 118. 28/06/2009 Adding a recordset to find the id value • Create this recordset to find the largest number. Creating a new journal ID • Find the recordset code as shown above. • Add lines 61 & 62 li 62. • These lines obtain the highest id and add one to it. Database php version 2.1 118
  • 119. 28/06/2009 Storing the id in the form. • Add a hidden textfield in the form as shown shown. • Write the value as shown. This is the value of the new id that was calculated at the top of the page on the previous slide. Placing the insert record script • Double click on the insert record made earlier and add the new id as the journalID. Database php version 2.1 119
  • 120. 28/06/2009 Session Variables Creating a Session variable • Create a basic page as shown with a textfield and button in a form. • Create a new page and call it createsession.php. • Link the form to the createsession.php page. Database php version 2.1 120
  • 121. 28/06/2009 Creating a Session variable - 2 • To create a session variable the session_start() function must be the 1st line in _ () the document. • Then declare and initialise the variable as shown below. Post from the page before. Creating a Session variable - 3 • Create the page below, starting the session and then displaying it. p y g • Create a link on the createsession.php to this page. This is show on the previous slides. Database php version 2.1 121
  • 122. 28/06/2009 Classes in php Creating A Class • Classes are similar to Java and actionscript. • A class has a constructor or a default on is added at compilation time. • Below is the start of a person class. • This class is to hold the info about the people that log into the following pages. Name of the class and file name must be identical. Declaring D l i variables. i bl Constructor must be identical to the class name. Database php version 2.1 122
  • 123. 28/06/2009 Creating a Class -2 • The gets and the sets. • The close of the class • The close of the php tag. Using the class to store variables • The next couple of slides show the code to create a session variable so it can store an array of objects. • These objects are created using the Person class. Database php version 2.1 123
  • 124. 28/06/2009 Using the class to store variables-2 • A login page to start the session variable. Using the class to store variables-3 • The design view of the add people p g page. Database php version 2.1 124
  • 125. 28/06/2009 Using the class to store variables-4 • The php code. The include statement allows access to the Person class. l Using the class to store variables-5 • The html code on the page. Database php version 2.1 125
  • 126. 28/06/2009 Using the class to store variables-6 • How to view the data in the class. Using the class to store variables-7 Table headings. Obtaining the number of objects in the array. Getting each variable for each object. Placing each variable in the table Database php version 2.1 126
  • 127. 28/06/2009 Using the class to store variables-8 • These files can be found a zipped folder called classes.zip on p myChisholm. Database php version 2.1 127