CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.

Slides:



Advertisements
Similar presentations
 HTTP Communication  HTTP Communication for Web  Static  Dynamic  Dynamic Content  PHP  PHP Block  PHP Programming Style  Database  MySQL.
Advertisements

Slide 1 of 40 PHP Form Handling The PHP superglobals $_GET and $_POST are used to collect form-data. EX: Name: CENG 449 Lecture 11.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
PHP & MySQL Mahak Arora Vivek Bangera. Outline How PHP works Basic scripting in PHP Forms in PHP(GET & POST Variables) SQL basics PHP and MySQL connection.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
04/09/20151 PHP & MySQL 'Slide materials are based on W3Schools PHP tutorial, 'PHP website 'MySQL website.
Unit 7 – Working with Forms 1. Creating a form 2. Accessing the submitted data 3. Common operations on forms.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Form Handling.
PHP Forms and User Input The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
INTERNET APPLICATION DEVELOPMENT For More visit:
PHP / MySQL. What is PHP? PHP is an acronym for "PHP Hypertext Preprocessor" PHP is a widely-used, open source scripting language PHP scripts are executed.
LEARN THE QUICK AND EASY WAY! VISUAL QUICKPRO GUIDE Chapter 2: Programming with PHP Copyright © 2012 by Larry Ullman Dr. Mogeeb Mosleh Saturday ( pm)
Week 7. Lecture 3 PHP Forms. PHP forms In part 2 of this course, we discussed html forms, php form is similar. Lets do a quick recap of the things we.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Slide 1 of 40 PHP Form Handling The PHP superglobals $_GET and $_POST are used to collect form-data. EX: Name: CENG 449 Lecture 11.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Array & Foreach อาร์เรย์และคำสั่งวนลูป. Content 1. Definition and Usage 2. Syntax 3. print_r() Statement 4. For and Foreach 5. Array Functions.
CS320 Web and Internet Programming Introduction to PHP Chengyu Sun California State University, Los Angeles.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Week 4 PHP H ypertext P reprocessor Reference : Official Site :
Slide 1 PHP Arrays and User Defined Functions ITWA133.
Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope.
Server-Side Scripting with PHP ISYS 475. PHP Manual Website
Outline if...else...elseif Statements Switch Loops Functions Arrays Forms.
Chapter 2 Programming with PHP Part 3. handle_form.php Script 2.5 on page 56 orm.html
Part 2 Lecture 9 PHP Superglobals and Form Handling.
PHP. PHP User Defined Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used.
הרצאה 4. עיבוד של דף אינטרנט דינמי מתוך Murach’s PHP and MySQL by Joel Murach and Ray Harris.  דף אינטרנט דינמי משתנה עפ " י הרצת קוד על השרת, יכול להשתנות.
12 – PHP Contd. Informatics Department Parahyangan Catholic University.
Since you’ll need a place for the user to enter a search query. Every form must have these basic components: – The submission type defined with the method.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
 An array stores multiple values in one single variable.  Example: Output: I like Honda Civic, BMW and Toyota.
NMD202 Web Scripting Week2. Web site
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
11 – Introduction to PHP(1) Informatics Department Parahyangan Catholic University.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
CGS 3066: Web Programming and Design Spring 2016 PHP.
PHP and Form Processing CS3520. Idea We have an HTML form that when user clicks on it in their browser a CGI HTTP request is created by the browser and.
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
Web Programming with PHP (3) Superglobals, Form & File processing.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
PHP using MySQL Database for Web Development (part II)
Web Systems & Technologies
CGS 3066: Web Programming and Design Spring 2017
Session 2 Basics of PHP.
Creating php Pages Done By: Hind Talafha.
Pemrograman WEB I Pertemuan 6.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
DBW - PHP DBW2017.
CHAPTER 5 SERVER SIDE SCRIPTING
PHP Functions Besides the built-in PHP functions, we can create our own functions. A function is a block of statements that can be used repeatedly in.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
8th Semester, Batch 2008 Department of Computer Science SSUET.
Ch. 3. PHP (이 강의 내용의 대부분 예들은 w3schools. com/php/default
Introduction to Web programming
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
PHP an introduction.
PHP Lecture 11 Kanida Sinmai
PHP Array.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

CHAPTER 7 Introduction to PHP5 Part II อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1

Content PHP 5 if...else...elseif Statements PHP 5 switch Statement PHP 5 while Loops PHP 5 for Loops PHP 5 Functions PHP 5 Arrays PHP 5 Sorting Arrays PHP 5 Global Variables - Superglobals 2

Content PHP 5 Form Handling PHP 5 Form Validation PHP 5 Forms - Required Fields PHP 5 Forms - Validate and URL PHP 5 Complete Form Example 3

PHP 5 if...else...elseif Statements PHP - The if Statement 4 PHP - The if...else Statement <?php $t = 15; if ($t < 20) { echo "Have a good day!"; }else{ echo "Have a good night!"; } ?>

PHP 5 if...else...elseif Statements (cont.) PHP - The if...elseif....else Statement 5

PHP 5 switch Statement The PHP switch Statement 6

PHP 5 while Loops The PHP while Loop "; $x++; } ?> 7 The PHP do...while Loop "; $x++; } while ($x

PHP 5 for Loops The PHP for Loop "; } ?> 8 The PHP foreach Loop "; } ?>

PHP 5 Functions Create a User Defined Function in PHP Syntax function functionName() { code to be executed; } Example 9

PHP 5 Functions (cont.) PHP Function Arguments Example "; } familyName("Jani"); familyName("Hege"); //output Jani Hege ?> 10

PHP 5 Functions (cont.) PHP Default Argument Value Example "; } setHeight(350); setHeight(); // will use the default value of 50 setHeight(135); setHeight(80); ?> 11

PHP 5 Functions (cont.) PHP Functions - Returning values Example "; echo " = ". sum(7, 13). " "; echo "2 + 4 = ". sum(2, 4); ?> 12

PHP 5 Arrays What is an Array? An array is a special variable, which can hold more than one value at a time. $cars1 = "Volvo"; $cars2 = "BMW"; $cars3 = "Toyota"; However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300? 13

PHP 5 Arrays (cont.) Create an Array in PHP array(); Example $cars = array("Volvo", "BMW", "Toyota"); or the index can be assigned manually: $cars[0] = "Volvo"; $cars[1] = "BMW"; $cars[2] = "Toyota"; 14

PHP 5 Arrays (cont.) Get The Length of an Array - The count() Function count(); Example //output 3 15

PHP 5 Arrays (cont.) Loop Through an Indexed Array Example "; } ?> 16

PHP 5 Arrays (cont.) PHP Associative Arrays Example "35", "Ben"=>"37", "Joe"=>"43"); echo "Peter is ". $age['Peter']. " years old."; ?> 17 Loop Through an Associative Array Example "35", "Ben"=>"37", "Joe"=>"43"); foreach($age as $x => $x_value) { echo "Key=". $x. ", Value=". $x_value; echo " "; } ?>

PHP 5 Sorting Arrays PHP - Sort Functions For Arrays In this chapter, we will go through the following PHP array sort functions: sort() - sort arrays in ascending order rsort() - sort arrays in descending order asort() - sort associative arrays in ascending order, according to the value ksort() - sort associative arrays in ascending order, according to the key arsort() - sort associative arrays in descending order, according to the value krsort() - sort associative arrays in descending order, according to the key 18

PHP 5 Sorting Arrays (cont.) Sort Array in Ascending Order - sort() 19 Sort Array in Descending Order - rsort()

PHP 5 Sorting Arrays (cont.) Sort Array (Ascending Order), According to Value - asort() "35", "Ben"=>"37", "Joe"=>"43"); asort($age); ?> 20 Sort Array (Ascending Order), According to Key - ksort() "35", "Ben"=>"37", "Joe"=>"43"); ksort($age); ?>

PHP 5 Sorting Arrays (cont.) Sort Array (Descending Order), According to Value - arsort() "35", "Ben"=>"37", "Joe"=>"43"); arsort($age); ?> 21 Sort Array (Descending Order), According to Key - krsort() "35", "Ben"=>"37", "Joe"=>"43"); krsort($age); ?>

PHP 5 Global Variables - Superglobals PHP Global Variables - Superglobals Several predefined variables in PHP are "superglobals", which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. The PHP superglobal variables are: $GLOBALS $_SERVER $_REQUEST $_POST $_GET $_FILES $_ENV $_COOKIE $_SESSION 22

PHP 5 Global Variables - Superglobals (cont.) PHP $_POST "> Name: 23

PHP 5 Global Variables - Superglobals (cont.) PHP $_GET //test_get.html Test $GET 24 PHP $_GET //test_get.php

PHP 5 Form Handling PHP - A Simple HTML Form $_GET Name: 25 PHP - A Simple HTML Form Welcome Your address is: // output Welcome name Your address is The PHP superglobals $_GET and $_POST are used to collect form-data.

PHP 5 Form Handling PHP - A Simple HTML Form $_POST Name: 26 PHP - A Simple HTML Form Welcome Your address is: // output Welcome name Your address is

PHP 5 Form Validation 27

PHP 5 Form Validation (cont.) 28 // formValidate.php <?php $name = $ = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { $name = test_input($_POST["name"]); $ = test_input($_POST[" "]); $website = test_input($_POST["website"]); $comment = test_input($_POST["comment"]); $gender = test_input($_POST["gender"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>

PHP 5 Form Validation (cont.) 29 // formValidate.php PHP Form Validation Example "> Name: Website: Comment: Gender: Female Male

PHP 5 Form Validation (cont.) 30 // formValidate.php Your Input: "; echo $name; echo " "; echo $ ; echo " "; echo $website; echo " "; echo $comment; echo " "; echo $gender; ?>

PHP 5 Forms - Required Fields 31 // formValidate.php <?php $nameErr = $ Err = $genderErr = $websiteErr = ""; $name = $ = $gender = $comment = $website = ""; if ($_SERVER["REQUEST_METHOD"] == "POST") { if (empty($_POST["name"])) { $nameErr = "Name is required"; } else { $name = test_input($_POST["name"]); } if (empty($_POST[" "])) { $ Err = " is required"; } else { $ = test_input($_POST[" "]); }

PHP 5 Forms - Required Fields (cont.) 32 // formValidate.php if (empty($_POST["website"])) { $website = ""; } else { $website = test_input($_POST["website"]); } if (empty($_POST["comment"])) { $comment = ""; } else { $comment = test_input($_POST["comment"]); } if (empty($_POST["gender"])) { $genderErr = "Gender is required"; } else { $gender = test_input($_POST["gender"]); } } ?>

PHP 5 Forms - Required Fields (cont.) 33 // formValidate.php "> Name: * * Website: Comment: Gender: Female Male *

PHP 5 Forms - Validate and URL 34 // formValidate.php PHP - Validate Name $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "Only letters and white space allowed"; } // formValidate.php PHP - Validate $ = test_input($_POST[" "]); if (!filter_var($ , FILTER_VALIDATE_ )) { $ Err = "Invalid format"; }

PHP 5 Forms - Validate and URL (cont.) 35 // formValidate.php PHP - Validate URL $website = test_input($_POST["website"]); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0- { $websiteErr = "Invalid URL"; }

PHP 5 Complete Form Example 36 // formValidate.php PHP - Keep The Values in The Form Name: "> "> Website: "> Comment: Gender: value="female">Female value="male">Male

37 THE END