Download presentation
Presentation is loading. Please wait.
Published byChastity Nash Modified over 9 years ago
2
Website Development with PHP and MySQL Saving Data
3
What you will achieve this week! Submitting data to the server Saving data on a server using files
4
: Customer browser request service access page interpret set data present html return html get data databasescripting language web server Reminder of the general process
5
What can we do with Forms? Standard HTML tags which: –Collect user input –Specify where to send the input –Specify how to send the input to be processed HTML tags simply provide an interface to allow user interaction Input is handled by a separate script or program: –On the server e.g. via PHP, ASP, CGI etc. –In the browser e.g. JavaScript
6
More Input Types Radio buttons Choose ONE of the following: A B C Check boxes Choose ANY, ALL or NONE of the following: A B C
7
Even More Input Types Drop-down menus Text area
8
Secret Stuff? Passwords Care to give us a password? Hidden fields
9
Submit and Reset B uttons The submit button sends form data to be processed The reset button returns fields to original state
10
How to get data from a form to a program Specify how to send the form data –Specified with the method attribute –GET or POST or –If not specified – default is GET Specify where to send the form data –Specified by the action attribute –Usually a script or program – can be local or full URL
11
The GET Method Client makes an HTTP GET request for a script –Form data is appended to the script URL as name/value pairs – a querystring –The script has access to values sent in the querystring In addition –Input data can be hard-coded directly in a URL No need for a form –Results can be bookmarked
12
The POST Method Client makes an HTTP POST request for a script –Form data is sent as name/value pairs in the body of the request In addition –Useful for large amounts of data Browser may truncate a long querystring –Data is not visible in the URL –Results cannot be bookmarked
13
Form Processing Scripts Scripts are run by the web server via a web server application –CGI, PHP, ASP, ColdFusion etc… Scripts can be written in almost any language –PHP, VBScript, Perl, CFML etc… –Language may depend on web server application(s) Scripts receive form input from via GET or POST –Process input based on instructions in the script Create an e-mail message Perform database query Write information to a file Generate a web page
14
Form Processing Query database? Name: Arnold Comments: “Nice website!"... User submits form to web server Web server passes form data to processing script Write a file? Send email? Return a web page?
15
Submitting data to server through forms Simple examples that echo the submitted data back
16
: customer simpleTextBox.htmwebServerechoName.php enter name submit submit(echoName.php3) open($name) display
17
Name test a PHP page to process the form a variable to pass to the PHP page
18
echoName.php
19
: customer calculator.htmwebServercalculatorResult.php enter numbers submit submit(calculatorResult.php3) open($num1,$num2) display
20
Number 1 Number 2 test
21
calculatorResult.php
22
Saving data using files
23
: customer nameAddress.htmwebServersaveAddress.phpnameAddress enter name enter address submit submit(saveAddress.php) open($name,$address) open write close display
24
int fopen (string filename, string mode) mode may be any of the following: 'r' - Open for reading only; place the file pointer at the beginning of the file. 'r+' - Open for reading and writing; place the file pointer at the beginning of the file. 'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it. 'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
25
string sprintf (string format [, mixed args...]) The format string is composed of zero or more directives: ordinary characters (excluding %) that are copied directly to the result, and conversion specifications, each of which results in fetching its own parameter. b - the argument is treated as an integer, and presented as a binary number. c - the argument is treated as an integer, and presented as the character with that ASCII value. d - the argument is treated as an integer, and presented as a decimal number. f - the argument is treated as a double, and presented as a floating-point number. o - the argument is treated as an integer, and presented as an octal number. s - the argument is treated as and presented as a string. e.g. sprintf(“%d is the number of %s in a %y”, 12, “months”, “year”) results in “12 is the number of months in a year”
26
Name Address Test
27
saveAddress.php
28
Guestbook Application We need: A form A file (or database table) to hold the data entered via the form Some means of reading the data from the file so that we can see it We will look into this case study further when we move onto databases.
29
So, what have we got so far? A means of collecting data from the user A means of transmitting it to the server A means of recording it on the server
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.