+ All Categories
Home > Documents > Simple PHP Calculator

Simple PHP Calculator

Date post: 05-Apr-2018
Category:
Upload: kishan-chand
View: 216 times
Download: 0 times
Share this document with a friend

of 30

Transcript
  • 8/2/2019 Simple PHP Calculator

    1/30

    Simple PHP application

  • 8/2/2019 Simple PHP Calculator

    2/30

    A simple application

    We are going to develop a simple PHPapplication with a Web interface.

    The user enters two numbers and theapplication returns the two multiplied totogether

    Start

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htm
  • 8/2/2019 Simple PHP Calculator

    3/30

    UML Interaction Diagram

    Interaction diagrams describe thecommunication between objects, but wewill use them to describe interationbetween programs even if they are notobjects

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htm
  • 8/2/2019 Simple PHP Calculator

    4/30

    Simple interaction diagram

    Simple example and explanation Ref to UML

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htm
  • 8/2/2019 Simple PHP Calculator

    5/30

    User-script interactionBrowser

    Web server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    multiply.phpPHP processor

    run script

    5 * 6 = 30

    5 * 6 = 30HTMLMIME

    User

    Enter data

    Read output

    get form.htmClick link

    Locate file

    Calculate button

    x=5 y=6

    Displayform.htm

    Display generated HTML

    page

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/start.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htm
  • 8/2/2019 Simple PHP Calculator

    6/30

    User-browser interactionBrowserUser

    Enter data

    Read output

    Click link

    Calculate button

    Display form.htm

    Display generated HTML

    page

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htmhttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/form.htm
  • 8/2/2019 Simple PHP Calculator

    7/30

    User Browser interaction

    User locates desired link Multiply numbers

    Browser retrieves the form from the server Form is displayed User fills in the fields on the form User clicks submit button

    Magic stuff happens User reads the result and goes back to the form

    to change the values in the form

  • 8/2/2019 Simple PHP Calculator

    8/30

    form.htm

    x =

    y =

  • 8/2/2019 Simple PHP Calculator

    9/30

    Browser-Server interactionBrowser

    Web server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    5 * 6 = 30

    Click button

  • 8/2/2019 Simple PHP Calculator

    10/30

    Browser-Server interaction

    Parameters passed as data couplets (name/value pairs)either Attached to URL (and visible to user in the address line)

    Values must be URL-Encoded space to +

    punctuation to %hex e.g. ? to %3f METHOD=GET in an HTML Form

    appended to the ACTION URL

    Explicitly added to the base URL e.g. 5 * 6

    A separate document (invisible to user)

    METHOD=POST in an HTML Form Result passed back in a MIME wrapper

    MIME tells browser what kind of file it is (HTML,JPEG, XML,Flash.. )

    Content-type: image/jpeg

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply.php?x=5&y=6
  • 8/2/2019 Simple PHP Calculator

    11/30

    Server-script interactionWeb server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    multiply.php

    run script

    5 * 6 = 30

    x=5 y=6

  • 8/2/2019 Simple PHP Calculator

    12/30

    User-script interactionBrowser

    Web server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    multiply.phpPHP processor

    run script

    5 * 6 = 30

    5 * 6 = 30HTMLMIME

    User

    Enter data

    Read output

    get form.htmClick link

    Locate file

    Calculate button

    x=5 y=6

    Display form.htm

    Display generated HTML

    page

  • 8/2/2019 Simple PHP Calculator

    13/30

    form.htm

    x =

    y =

  • 8/2/2019 Simple PHP Calculator

    14/30

    Browser-Server interactionBrowser

    Web server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    5 * 6 = 30HTMLMIME

    Click button

  • 8/2/2019 Simple PHP Calculator

    15/30

    URL

    Relative URL multiply.php?x=5&y=6

    Absolute URL http://localhost/UFIE8V/l3php/multiply.php?x=5&y=6 (my

    Home machine running IIS)

    http://stocks.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.ph

    (the development server)

    http://www.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php(the public production server)

    http://localhost/UFIE8V/l3php/multiply.php?x=5&y=6http://stocks.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6http://www.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6http://www.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6http://stocks.uwe.ac.uk/~cjwallac/UFIE8V/l3php/multiply.php?x=5&y=6http://localhost/UFIE8V/l3php/multiply.php?x=5&y=6
  • 8/2/2019 Simple PHP Calculator

    16/30

    Server-script interactionWeb server

    get multiply.php?x=5&y=6

    Locate file and runas PHP

    multiply.phpPHP processor

    run script

    5 * 6 = 30

    x=5 y=6

  • 8/2/2019 Simple PHP Calculator

    17/30

    Server-PHP interaction

    Parameters GET and POST Parameters available as variables of the same name $x, $y (deprecated but used in my code ) in an array $_GET[x] depends on the setup

    Parameter values are URL-Decoded

    Implicit Parameters In addition to the user-defined parameters, other data is available aboutthe client and about the server

    $PHP_SELF is the program name $HTTP_USER_AGENT is the browser

    Reply

    HTML goes through to output script is executed as PHP

    Print or echo statements add to output

    All output returned to Server Other kinds of output (JPEG, GIF.. ) require different headers to be

    output

  • 8/2/2019 Simple PHP Calculator

    18/30

    Multiply.php script

  • 8/2/2019 Simple PHP Calculator

    19/30

    The generated HTML

    5 * 6 = 30

    This simple text is not XHTML compliant ofcourse.

    HTML to provide a readable page can be added

    around the PHP 5 * 6

    Note that we have added the name/value pairsto the URL not very useful here obviously.

    some pairs can be in the action URL of a form,some in input fields

    The script multiply2.php includes the originalPHP script within an HTML page

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply2.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply2.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply2.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply2.php?x=5&y=6http://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply2.php?x=5&y=6
  • 8/2/2019 Simple PHP Calculator

    20/30

    Multiply2.phpProgram composition with require

    PHP introduction - multiply script

    PHP introduction

  • 8/2/2019 Simple PHP Calculator

    21/30

    Sticky Forms

    Poor interface -user must to go back tooriginal form to change the data and re-calculate.

    Key idea: Combine form and calculator into one script

    Do calculation (if required) first

    Put inputs as defaults in form Simple combination of form and script

    Calculate Product

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.php
  • 8/2/2019 Simple PHP Calculator

    22/30

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.php
  • 8/2/2019 Simple PHP Calculator

    23/30

    multiply3.phpCombined form and calculator

  • 8/2/2019 Simple PHP Calculator

    24/30

    Initial Form

  • 8/2/2019 Simple PHP Calculator

    25/30

    Form with Entered Values

  • 8/2/2019 Simple PHP Calculator

    26/30

    Forms processing

    Interface and presentation can be easilyimproved Calculate Product

    Script can do validation and add error messagesto the form where fields are missing or invalid.

    Where to do the processing?:

    on Client in Javascript

    on Server in PHP or Java

    What factors do you need to consider to decide?

    http://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/mulform.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply4.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply4.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply4.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply4.phphttp://var/www/apps/conversion/current/tmp/scratch25577/scripts/multiply4.php
  • 8/2/2019 Simple PHP Calculator

    27/30

  • 8/2/2019 Simple PHP Calculator

    28/30

    SMS version

    Now nearly the same application with an SMSpresentation layer.

    For variety, Ive generalised to allow any number ofnumbers to be multiplied together

    Text MUL num1 num2 num3

    To:

    076 24 80 37 59

    Eg. MUL 34 56 78

    Reply 34 x 56 x 78 = 148512

  • 8/2/2019 Simple PHP Calculator

    29/30

    SMS to script interface

    Script plugs into SMS server Must obey the protocol:

    Parameters Text

    From Code

    Reply text: Reply:

    Entry required in routing file: MUL

    http://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.php

    http://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.php
  • 8/2/2019 Simple PHP Calculator

    30/30

    smsmult.php

    http://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.phphttp://www.cems.uwe.ac.uk/~cjwallac/ISD3/smsmult.php

Recommended