+ All Categories
Home > Documents > Website Development with PHP and MySQL Saving Data.

Website Development with PHP and MySQL Saving Data.

Date post: 02-Jan-2016
Category:
Upload: chastity-nash
View: 223 times
Download: 6 times
Share this document with a friend
Popular Tags:
28
Website Development with PHP and MySQL Saving Data
Transcript
Page 1: Website Development with PHP and MySQL Saving Data.

Website Development with PHP and MySQL

Saving Data

Page 2: Website Development with PHP and MySQL Saving Data.

What you will achieve this week!

•Submitting data to the server•Saving data on a server using files

Page 3: Website Development with PHP and MySQL Saving Data.

: Customer browser

request service

access page

interpretset data

present html

return html

get data

get data

databasescripting language

web server

Reminder of the general process

Page 4: Website Development with PHP and MySQL Saving Data.

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

Page 5: Website Development with PHP and MySQL Saving Data.

More Input Types• Radio buttons Choose ONE of the following: <input type="radio" name="choice" value="A"

checked="checked" />A

<input type="radio" name="choice" value="B" />B

<input type="radio" name="choice" value="C" />C

• Check boxes Choose ANY, ALL or NONE of the following: <input type="checkbox" name="A" value="yes"

checked="checked" />A

<input type="checkbox" name="B" value="yes" />B

<input type="checkbox" name="C" value="yes" />C

Page 6: Website Development with PHP and MySQL Saving Data.

Even More Input Types

• Drop-down menus

• Text area

Page 7: Website Development with PHP and MySQL Saving Data.

Secret Stuff?

• Passwords<p>

Care to give us a password?

<input type="password" name="my_password" />

</p>

• Hidden fields<p>

<input type="hidden" name="my_secret" value="hello" />

</p>

Page 8: Website Development with PHP and MySQL Saving Data.

Submit and Reset Buttons

• The submit button sends form data to be processed<p> <input type="submit" value="submit" /></p>

• The reset button returns fields to original state<p> <input type="reset" value="reset" /></p>

Page 9: Website Development with PHP and MySQL Saving Data.

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<form method="get"> or <form method="post">

– 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<form method="get" action="formscript.php">

<form method="get" action="http://www.hud.ac.uk/resources/scripts/formscript.php">

Page 10: Website Development with PHP and MySQL Saving Data.

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

Page 11: Website Development with PHP and MySQL Saving Data.

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

Page 12: Website Development with PHP and MySQL Saving Data.

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

Page 13: Website Development with PHP and MySQL Saving Data.

Form Processing

Query database?

Name: ArnoldComments:“Nice website!"

<html>...</html>

User submits form to web server

Web server passes form data to processing script

Write a

file?

Send email?

Return a web page?

Page 14: Website Development with PHP and MySQL Saving Data.

Submitting data to server through forms

Simple examples that echo the submitted data back

Page 15: Website Development with PHP and MySQL Saving Data.

: customersimpleTextBox.htm webServer echoName.php

enter name

submitsubmit(echoName.php3) open($name)

display

Page 16: Website Development with PHP and MySQL Saving Data.

<html>

<head></head>

<body>

<form method="POST" action="echoName.php"> <p>Name<input type="text" name="name" size="20"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p></form>

</body>

</html>

test

a PHP page to process the form

a variable to pass to the PHP page

Page 17: Website Development with PHP and MySQL Saving Data.

echoName.php

Page 18: Website Development with PHP and MySQL Saving Data.

: customercalculator.htm webServer calculatorResult.php

enter numbers

submitsubmit(calculatorResult.php3) open($num1,$num2)

display

Page 19: Website Development with PHP and MySQL Saving Data.

<html>

<head></head>

<body>

<form method="POST" action="calculatorResult.php"> <p>Number 1<input type="text" name="num1" size="20"></p> <p>Number 2<input type="text" name="num2" size="20"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p></form>

</body>

</html>

test

Page 20: Website Development with PHP and MySQL Saving Data.

calculatorResult.php

Page 21: Website Development with PHP and MySQL Saving Data.

Saving data

using files

Page 22: Website Development with PHP and MySQL Saving Data.

: customernameAddress.htm webServer saveAddress.php nameAddress

enter name

enter address

submitsubmit(saveAddress.php) open($name,$address) open

write

close

display

Page 23: Website Development with PHP and MySQL Saving Data.

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.

Page 24: Website Development with PHP and MySQL Saving Data.

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”

Page 25: Website Development with PHP and MySQL Saving Data.

<html>

<head></head>

<body>

<form method="POST" action="saveAddress.php"> <p>Name <input type="text" name="name" size="40"></p> <p>Address <input type="text" name="address" size="60"></p> <p><input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p></form>

</body>

</html>

Test

Page 26: Website Development with PHP and MySQL Saving Data.

saveAddress.php

Page 27: Website Development with PHP and MySQL Saving Data.

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.

Page 28: Website Development with PHP and MySQL Saving Data.

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


Recommended