Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS.

Similar presentations


Presentation on theme: "CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS."— Presentation transcript:

1 CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS

2 FORM VALIDATION Useful functions: NameDescriptionBest use empty($var)Returns TRUE if the variable has been set and is not NULL Text input isset($var)Returns TRUE if the variable hasn't been set, contains a NULL value, or contains an empty string. Non-text input: radio buttons, check boxes, submit. etc. is_numeric ($var)Returns TRUE if the variable is a number or a string that can be converted to a number

3 FORM HANDLING REVISITED Recall from Chapter 2 form handling with two pages: One page displays the form The second page processes it Instead, both parts can be written into one page with a conditional: if the form has been submitted process the data else display the form

4 BOOK EXAMPLE REVISED Trip Cost Calculator Complete code:

5 REVISED BOOK EXAMPLE WITH FORM VALIDATION // Check for form submission: if (isset($_POST['submit'])) { // Minimal form validation: if (isset($_POST['distance'], $_POST['gallon_price'], $_POST['efficiency']) && is_numeric($_POST['distance']) && is_numeric($_POST['gallon_price']) && is_numeric($_POST['efficiency']) ) { //calculate and print results } else { //invalid input echo ' Error! Please enter a valid distance, price per gallon, and fuel efficiency. ';} } Complete code:

6 STICKY FORMS The form remembers your previous entries. Text input: use the value attribute to print the variable if it exists. Radio buttons or check-boxes: add the code checked="checked" to the input tags if that value was selected. Textarea input: since there is no value attribute, just print the variable (if it is empty, nothing will show.) To preset a selection list, use the selected attribute. Complete code:

7 INCORPORATING EXTERNAL FILES include() and require() are equivalent when there are no problems – they differ in how they handle errors: include() – if include() fails, a warning will display, but the script will continue to run require() – if require() fails, and error is displayed, and the script is halted Relative or absolute referencing may be used – relative is preferable include_once() and require_once() are available for more complex code but cause extra work and thus potential slow- downs

8 ABSOLUTE VS RELATIVE PATHS An absolute path references a file starting from the root directory of the server: include('C:/php/includes/file.php'); include('/usr/home/public_html/php/includes/file.php'); A relative path uses the referencing (parent) file as the starting point. It will remain accurate even if the site is moved to another server. To move up one folder, use two periods To move into a folder, use its name followed by / include('../ex2/file.php');

9 SEPARATING PAGE PARTS

10 header.html

11 footer.html

12 index.php

13 A SIMPLE FILE STRUCTURE index.php includes (folder) header.htmlfooter.htmlstyle.css images (folder)

14 MORE COMPLEX FILE STRUCTURES Use MVC pattern: Model: contains files that manage the data or that interface with the database View: contains the files that represent the user interface Controller: contains files that receive the HTTP requests from browsers

15 USER-DEFINED FUNCTIONS Function names are case insensitive (unlike variable names) Syntax: function function_name() { // function code } Uses associate repeated code with one function call separate out sensitive or complicated processes from other code make common code bits easier to reuse

16 FUNCTIONS AND ARGUMENTS EXAMPLE Use a function to create the radio buttons in the car calculator program: // This function creates a radio button. // The function takes one argument: the value. // The function also makes the button "sticky". function create_gallon_radio($value) { // Start the element: echo ' $value "; // Complete the element } // End of create_gallon_radio() function.

17 CALLING THE FUNCTION Distance (in miles): " /> Ave. Price Per Gallon:

18 RETURNING VALUES FROM FUNCTIONS PHP functions may or may not return values print will return a 1 (success) or a 0 (fail) echo does not return anything Use return() Example: function calculate_trip_cost($miles, $mpg, $ppg) { // Get the number of gallons: $gallons = $miles/$mpg; // Get the cost of those gallons: $dollars = $gallons/$ppg; // Return the formatted cost: return number_format($dollars, 2); } // End of calculate_trip_cost() function.


Download ppt "CHAPTER 3 MORE ON FORM HANDLING INCLUDING MULTIPLE FILES WRITING FUNCTIONS."

Similar presentations


Ads by Google