Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM336 – Web Database Development XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical.

Similar presentations


Presentation on theme: "COM336 – Web Database Development XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical."— Presentation transcript:

1 COM336 – Web Database Development XHTML & Forms

2 PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical usage of the form tag in HTML – id is optional (but encouraged in XHTML) action is mandatory. The value is the URL (or name of the file, if in current directory) where the data collected by the form is to be sent method can take one of two values: get and post 2

3 HTML forms HTML forms (a way of getting input from the browser) include form elements such as: – text areas – password areas – check boxes – selection lists – scrolled selection lists – radio buttons – submit button – reset button – picture buttons 3

4 An HTML form 4

5 5

6 Using method=“get” This method appends the form-data to the URL in name/value pairs http://localhost:8080/COM409/receive1.php?email=ytdrtyerh This method is useful for form submissions where a user want to bookmark the result (think google search) There is a limit to how much data you can place in a URL (varies between browsers), therefore, you cannot be sure that all of the form-data will be correctly transferred Never use the "get" method to pass sensitive information! (password or other sensitive information will be visible in the browser's address bar) 6

7 Using method=“post” This method sends the form-data as an HTTP post transaction Form submissions with the "post" method cannot be bookmarked (think login forms or member only areas) The "post" method is more robust and secure than "get", and "post" does not have size limitations 7

8 PHP Superglobals $_GET $_POST Works in the same way as $_GET – $_POST is an associative array – The keys to the array are the same as the field names in your form – Data sent via post is not exposed to the browser. The transaction is handled internally by the webserver 8

9 HTML form tags - overview 9

10 Starting and ending forms - tag HTML forms are created by using the HTML and tags Within these tags, you place various HTML form elements, such as text areas, check boxes, and radio buttons..  ---(Your FORM elements here). 10

11 Form arguments The tag has two primary arguments: action - Specifies the URL of the PHP script to start when the form is submitted method - Defines the format that will be used to send data to the PHP script – get appends the form arguments to the end of the Web address (as a Query String after the “?”) – post sends the data as part of the body of the HTTP Request 11

12 The tag Used to define a range of form elements, designated by its TYPE attribute. The general format of the tag is: – type = submit | reset | text | checkbox | radio| etc. – name: will be used by the CGI program to identify the form element (i.e. the CGI variable) – value: the value to be associated with “name” when processing the form (The tag has NO closing tag ) 12

13 The tag - Form buttons Used to submit the form or erase all input When the submit button is clicked, the form data is sent to the program specified in the ACTION argument, and the CGI program executed If form consists only of a submit button, the CGI program is simply executed 13

14 tag - Text box The text input type creates text boxes on forms 14 text area box size max. characters allowed CGI variable name

15 tag - Text box 15

16 Text areas - tag 16 u Enables the user to enter multiple lines of text Please enter comments here u The wrap attribute specifies what to do if the user types beyond the right margin of the text area If omitted, wrapping is not enabled If wrap=“virtual”, wrapping is enabled but the text is submitted without linebreaks If wrap=“physical”, line breaks are submitted as part of the text CGI variable name default text value

17 tag - Checkboxes 17 Copyright © 2002 Pearson Education, Inc.

18 tag - Checkboxes Small boxes on a form that create a check mark when the user clicks them USA China The user may select (tick) multiple boxes 18 CGI variable name CGI variable value when clicked label displayed next to checkbox

19 tag - Radio buttons 19 Copyright © 2002 Pearson Education, Inc.

20 Radio buttons - tag Small circles that operate similarly to checkboxes, except that only one button within the group can be selected at any given time Visa American Express Mastercard 20 CGI variable name CGI variable value label displayed next to radio button

21 tag - Selection lists Used to create a list for user to choose from, either as a drop- down menu or a scrolling box: Win95 Win98 Win2k linux For a scrolling box, specify SIZE greater than one. e.g. SIZE = 3 will generate a scrolling windows 3 rows high 21 CGI variable name list items that user can select CGI variable value

22 Form generation using PHP - Example 22

23 Form generation using PHP - Example - output 23

24 Example form Selection list Radio buttons Text box Text area Text box Submit button

25 PHP Same Page Processing A form can be sent to a different PHP page for processing, or can be processed on the same page. The action property of the form will determine who processes the form In order for a form to be processed on the same page, the page will have to be a PHP page and you will need to check for the submit button to be pressed. (not empty) – Also the Action of the form can be set to the same page or left empty.

26 Example <?php if (!empty($_POST['mailing-submit'])) { //Code to be executed upon processing the form; } ?> Adapted From: BAVOTA, C. (2009) Processing Multiple Forms on One Page with PHP [ONLINE] URL: http://bavotasan.com/2009/processing-multiple-forms-on-one-page-with-php/ It is a good idea to kill the script if the form is not filled by checking for set variables after the submit button is pressed

27 Example Multiple Forms Adapted From: BAVOTA, C. (2009) Processing Multiple Forms on One Page with PHP [ONLINE] URL: http://bavotasan.com/2009/processing-multiple-forms-on-one-page-with-php/

28 Multiple Form Processing – Same Page <?php if (!empty($_POST['mailing-submit'])) { //do something here to process the first form; } if (!empty($_POST['contact-submit'])) { //do something here to process the second form } ?> Adapted From: BAVOTA, C. (2009) Processing Multiple Forms on One Page with PHP [ONLINE] URL: http://bavotasan.com/2009/processing-multiple-forms-on-one-page-with-php/

29 PHP – User Defined Functions

30 Functions Functions are self-contained units of a program designed to accomplish a specified task such as calculating a mortgage payment, retrieving data from a database, or checking for a valid input PHP provide a good number of built-in functions and we have seen some of them, but there is a time when the built in functions are no longer capable of accomplishing your programming goals – that’s when we start coding user defined functions

31 Functions By definition a function is a block of statements that not only perform some task, but can also return a value. A function is independent of the program and is not executed until called

32 Declaration, Definition and Invocation Declaration: Is to give it a name along with the keyword function and a set of parenthesis that might contain parameters(arguments) that the function will accept. Definition: It’s the declaration AND the body of statements found within the curly braces after the function’s declaration Invocation: Refers to calling the function (tells PHP to start executing the statements withing the curly braces

33 Declaration, Definition and Invocation function do_something() // Declaration function do_something() { statement1;// Definition statement2; } do_something(); // Invocation

34 Where to Put Them Commonly used functions are often stored in separate fules to reuse as needed (using include or require PHP functions to load them) They can also be declared anywhere within a file and they are available only to the script where they are defined. Functions can be called before or after its definition (PHP internally compiles all function definitions before executing the statements in the script)

35 Function Example The following function Prints a custom message to the screen. Note that within curly braces, any legal PHP code is accepted, including other functions, calls, HTML, declaration of new variables, even calls to itself. <?php function bye() { print "Bye, Adios, Au Revoir, Adieu, fo, do svidaniya"; } ?>

36 Naming Conventions Function, like variables, has naming conventions: 1.It must consist of letters, digits, or the underscore and cannot begin with a digit. 2.The name must be unique for each function. 3.It should be given a name that indicates its purpose with an action word or a verb. 4.Function names are NOT case sensitive. Convention uses lowercase names.

37 Passing Arguments To send values to a function, it must be called (and defined) with a comma-separated list of arguments enclosed in parenthesis. The Arguments can be a combination of numbers, strings, references, variables etc. There are two ways of passing parameters to a function: by value and by reference

38 Passing Parameters by VALUE When passing a parameter by value, PHP makes a copy of the variable, so that if the function changes it inside the function, only the copy is being changed. When the function exits, the copy is destroyed. This is considered a safe way to pass arguments and is designed to be the default.

39 Example Calculate Volume: The Form Passing Arguments Find the volume (in feet) of a rectangular room with sides by by

40 The PHP <?php extract($_REQUEST); //$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. volume($s1, $s2, $s3); // Passing 3 arguments and calling the function function volume($side1,$side2,$side3){ print "The volume of the room is: ". $side1 * $side2 * $side3."cubic ft. "; } ?>

41 Passing Parameters By REFERENCE Only variables can be passed by reference. When passing values by reference, the function can change the value of the parameter being passed. To pass a parameter by reference, the variable being passed is preceded by the symbol ‘&’ in the function declaration line.

42 Example Parameters by Reference function html_tags(&$text, $tags="pre"){ switch($tags){ case 'br': $text = "$text "; break; case 'p': $text = " $text "; break; default: $text = " $text "; break; } This function receives 2 arguments: $text is passed by reference so the function will modify it $tags is passed as a value and it has a DEFAULT VALUE (pre) This function will format the string with the appropriate HTML tag in the appropriate place

43 Example Parameters by Reference Firs Call Example: $colors = array('red','green','blue'); $list=print_r ($colors,true); html_tags($list); echo "$list"; An array with 3 colors print_r will print the contents of the argument variable and save it to the variable $list then the function html_tags is called with only 1 argument, the list; the second parameter will take the value of PRE which is the DEFAULT Array ( [0] => red [1] => green [2] => blue ) Browser Output Browser Code View

44 Example Parameters by Reference Second Call Example: the string “Hello World” is going to be formatted here with tags so the “p” tag is passed by value and the $string by reference; the functions changes the string by adding the tags. notice the code view returned by the browser and the spacing in the browser output $string = "Hello, world!"; html_tags($string,"p"); echo "$string"; Hello, world! Hello, world! Browser Output Browser Code View

45 Returning Values Most of the time, a function will be constructed to make a calculation and return a value The RETURN statement in a functions allows the function to send a value back to the caller or to exit the function before it reaches the end.

46 Example - Return <?php $grades=array(40,90,89,93,75); $result = average($grades); // Pass an array print "The average grade is $result. "; function average($scores){ $sum = 0; $size = count($scores); // Find the size of the array if ( $size == 0 ){ echo "Empty parameter list "; die(); } // Exit the script here – exit(); can also be used here. for($i = 0; $i < $size; $i++){ $sum += $scores[$i]; //sums each score to the total ($sum) and stores in $sum } return $sum/$size; // Return the average } ?>


Download ppt "COM336 – Web Database Development XHTML & Forms. PHP and the WWW PHP and HTML forms – Forms are the main way users can interact with your PHP scrip Typical."

Similar presentations


Ads by Google