Lecture 6: Processing Forms with PHP

Slides:



Advertisements
Similar presentations
PHP Form and File Handling
Advertisements

File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Lecture 6/2/12. Forms and PHP The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input When dealing with HTML forms.
More on PHP Coding Lab no. 6 Advance Database Management System.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
Website Development Tutorial 3. Outline Getting data from the user using forms Sending data from a form to a PHP program Writing the data to a file for.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
HTML Form Processing Learning Web Design – Chapter 9, pp Squirrel Book – Chapter 11, pp
Python and Web Programming
CGI Programming: Part 1. What is CGI? CGI = Common Gateway Interface Provides a standardized way for web browsers to: –Call programs on a server. –Pass.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
(c) Manzur Ashraf, Short course, KFUPM PHP & MySQL 1 Basic PHP Class 2.
18. PHP File Operations Opening a File
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Website Development with PHP and MySQL Saving Data.
Chapter 6 Server-side Programming: Java Servlets
1 © Netskills Quality Internet Training, University of Newcastle HTML Forms © Netskills, Quality Internet Training, University of Newcastle Netskills is.
PHP2. PHP Form Handling The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. Name: Age:
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Storing and Retrieving Data
Outline Overview Opening a file How to create a file ? Closing a File Check End-of-file Reading a File Line by Line Reading a File Character by Character.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
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.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
CGS 3066: Web Programming and Design Spring 2016 PHP.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
COSC405 Web Application Engineering II Slides adopted from here
11 C File Processing.
In this session, you will learn to:
CGS 3066: Web Programming and Design Spring 2016
How to Write Web Forms By Mimi Opkins.
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
TMF1414 Introduction to Programming
1 CHAPTER 10 ADVANCED PHP.
Section 17.1 Section 17.2 Add an audio file using HTML
File I/O.
Arrays and files BIS1523 – Lecture 15.
Starting Out with Programming Logic & Design
Intro to PHP & Variables
MATLAB: Structures and File I/O
HTML Forms and User Input
Files I/O, Streams, I/O Redirection, Reading with fscanf
Chapter 11 – File Processing
Lecture 13 Input/Output Files.
Functions BIS1523 – Lecture 17.
Web Systems Development (CSC-215)
<?php require("header.htm"); ?>
File IO and Strings CIS 40 – Introduction to Programming in Python
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Text and Binary File Processing
File Input and Output.
File Handling.
Fundamental of Programming (C)
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Lecture 5: Functions and Parameters
Files.
Intro to PHP.
PHP Forms and Databases.
PHP: Hypertext Preprocessor
PHP an introduction.
Advanced Concepts and AJAX
PHP-II.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Lecture 6: Processing Forms with PHP Using PHP with forms is fairly simple When forms are submitted the server executes the php script, returning the resulting html Submitted form variables can be accessed from the $_POST array (or the $_GET array) The form element name is the key into the array $_REQUEST array Contains contents of $_POST, $_GET, $_COOKIE We’ll talk about $_COOKIE later Discuss and see getpost.php

Lecture 6: Processing Forms with PHP We can also use PHP to create forms However, it is really just HTML that we are using We can "interleave" the PHP and html to get the desired overall result, or we can have PHP output the appropriate HTML tags So if you don't know it yet – learn some HTML See Chapter 2 in Sebesta

Using files in PHP is fairly straightforward Lecture 6: PHP Files Using files in PHP is fairly straightforward Can open a file for reading, writing, append, and a couple variations of reading+writing Note 1: Files are not covered in the Sebesta text Note 2: You may have to set some permissions on your file system to allow your server write access to files There are a few different ways to access files in PHP Many C file functions are almost identical in PHP Ex: fopen, fseek, fscanf, fgetc, fgets See the manual for complete list

Lecture 6: PHP Files Opening files Typically we use fopen() to open a file for either reading or writing $fp = fopen(<filename>, <mode>); Where <filename> is the path/name of a file that is accessible to the server Where <mode> specifies how the file will be accessed Ex: "r"  read only "r+"  read/write with pointer at beginning The above modes require the file to already exist

Reading from files "w"  write only Lecture 6: PHP Files "w"  write only "w+"  write / read, truncating previous file length to 0 For the above modes, the server will attempt to create the file if it does not exist. Also "a" and "a+" for append modes Reading from files For text files, we can read different amounts per read depending on our requirements Read a single character at a time Read the entire file into a single string Read the lines of the file into an array of strings Can also read binary data if necessary Ex: images, audio, etc.

Writing to files Examples: Lecture 6: PHP Files PHP allows all of these with various functions Look at the options in the manual See: http://php.net/manual/en/ref.filesystem.php Writing to files Most commonly done with fwrite Again see manual for details Examples: See readwrite.php

Lecture 6: PHP Files Flocking files See http://php.net/manual/en/function.flock.php The flock() function is called to restrict access to files (when necessary) to one “user” at a time If each “user” calls flock() prior to accessing a file pointer to the same file, only one will be allowed to access it at a time Why do we need this? Multiple users frequently access the same server Server typically spawns a separate process for each user

Consider the following scenario for process P1: Lecture 6: PHP Files These processes can execute in pseudo-parallel or in actual parallel depending on how the server is configured Consider the following scenario for process P1: Read a file into an array Update a value in the array Write the array back to the file What if process P2 writes to the file between P1's reading and writing? If used correctly, flock() can prevent this problem See flock.php

Note: This issue is difficult to test with localhost server access Lecture 6: PHP Files Note: This issue is difficult to test with localhost server access Practically speaking we can only submit one request at a time However an online server can be accessed via many clients, with many coinciding processes See demo of flock.php on cs1520.cs.pitt.edu See ex12.php, ex12b.php Note many comments! Note how the script interacts with the data file Note how the PHP and html are interleaved