HTML pages describe static web content
They are stored by and delivered by a web server
A browser receives these pages from a web server and renders the HTML markup into something a human being can read
PHP code allows for dynamic web content
When a browser requests a page from a server that in.... ID: 649301

DownloadNote - The PPT/PDF document "The basics of web programming PowerPoint..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.

The basics of web programming: Lecture #4: Processing form data from PHP

Slide2

The story so far

HTML pages describe static web content

They are stored by and delivered by a web server

A browser receives these pages from a web server and renders the HTML markup into something a human being can read

PHP code allows for dynamic web content

When a browser requests a page from a server that includes some PHP, the server runs the PHP code first

The PHP code will do some processing

The PHP code will result in some HTML markup

This can be different each time the page is requested!

The HTML then gets sent to the browser

Net result: the end user sees a web page that changes

Slide3

HTML forms

HTML forms let us get data from the end user of a web page/site

Form fields are encased in an enclosing <form> element

There are different types of form fields for things like single lines of free text, larger text areas, pull down lists, checkboxes and radio buttons

Slide4

What is a form?

Slide5

The best known form on the web

Slide6

Forms collect different forms of information

A form might collect

Free text information

e.g.

Name

,

Address, What do you think about the price of fish?

Selections from a pre-defined set of options

e.g.

Gender

A positive or negative response to a question

e.g.

Have you ever done any programming before?

Online forms are no different!

A variety of form controls exist to allow you to collect information from the user of a web page

Slide7

How web forms work

A form is presented to the user within their web browser.

The user fills in the form fields

Most forms will have a "submit" button. Upon clicking this, the data that has been entered into the form is sent to a server (often the server which is hosting the web site from which the form originated, but not always)

A

server-side process

(usually a page in something like PHP, Perl, Python or similar) will process the data from the form. It might use other data sources to do so, e.g. databases, other files on the server hard disk, etc.

The server-side process will then generate a new page of HTML which represents the response to the user - i.e. any feedback or results to their input

Slide8

How web forms work

A form is presented to the user within their web browser.The user fills in the form fieldsMost forms will have a "submit" button. Upon clicking this, the data that has been entered into the form is sent to a server (often the server which is hosting the web site from which the form originated, but not always)A server-side process (usually a page in something like PHP, Perl, Python or similar) will process the data from the form. It might use other data sources to do so, e.g. databases, other files on the server hard disk, etc.The server-side process will then generate a new page of HTML which represents the response to the user - i.e. any feedback or results to their input

2.5 Client-side Javascript processes the user's form field inputs and makes changes to the HTML elements of the current page to provide feedback to the user

Slide9

The <form> element

For any form that is going to be processed by a server, a

<form>

element must surround all of the form components

(think blocks within blocks)

The

<form>

element will have (at least) an

action

attribute:

<form action="

register.php

">

…individual form components would go here…

</form>

The

action

attribute gives the URL for a page that will process the data that comes from the form

Usually, this will be something written in PHP, Python, Java, Perl or some other web programming platform…

Slide10

Form controls: text input

The <input> element creates a variety of different form componentsAt its simplest, it will display a simple text field: Last name: <input type="text" name="username"/>This would display a simple, plain text field into which the user could type text:(Q: Why does the input field appear next to the text?)

Slide11

Form controls: text input

<input> elements should contain (at least) a type attribute and a name attribute<input type="text" name="username" />If the type is not given, then it assumes that the type should be text (i.e. a simple single line text field)The name uniquely identifies the fieldWhen the form is sent to a server for processing, the code that runs server-side knows which field is which based on the value of the name attributes

Slide12

Form controls: text input

There are a number of other attributes you can use on an input element

size

specifies how wide (in characters) the input element should be

I

f this is not given, 20 is usually assumed

You can also style and size your input elements with CSS

maxlength

specifies the maximum number of characters that can be physically typed into the input element

<input name="username"

maxlength

="6" size="4"

/>

Note that the

size

and

maxlength

can be different:

The user can happily type in more characters than the

size

(the text inside the input box will scroll)

However, if the user tries to type in more characters than the

maxlength

, the additional characters will be ignored

If the

size

is more than the

maxlength

, then there will be empty space at the right hand side of the text box

Slide13

Form controls: text input

You can also specify a default text value using the

value

attribute

<input name="username"

value="Paul"/>

If this is given, then when the form loads the text box will already be filled in with this value…

…although if the user chooses they can delete the value and replace it with one of their own…

Slide14

Form controls: passwords

Passwords are one of the few controls that don't have an equivalent on a paper-based formThey are pretty simple though:Password:<input type="password" name="pwd"/>You give a type of passwordThe only difference between password fields and standard text fields is that anything the user types in is not visible on the screen

Slide15

Form controls: larger text boxes

To create a multiple line text box, youneed to use the <textarea> element<textarea cols="30" rows="10" name="story">The cat was playing in the garden.</textarea>

Slide16

Form controls: larger text boxes

To create a multiple line text box, youneed to use the <textarea> element<textarea cols="30" rows="10" name="story">The cat was playing in the garden.</textarea>The rows and cols attributesspecify the height (rows) andwidth (cols) of the textarea respectively. These are specified in characters (so this example is 30x10)

Slide17

Form controls: larger text boxes

To create a multiple line text box, youneed to use the <textarea> element<textarea cols="30" rows="10" name="story" >The cat was playing in the garden.</textarea>As usual, the name attributespecifies the unique name ofthis text area and distinguishesit from other form components

Slide18

Form controls: larger text boxes

To create a multiple line text box, youneed to use the <textarea> element<textarea cols="30" rows="10" name="story">The cat was playing in the garden.</textarea>The text between the start andthe end tags will be the defaulttext in the text area when theform loadsAs with the value attribute on the input element, the user can delete this default text and type their own if they choose

Slide19

Form controls: radio buttons

Radio buttons allow the user to choose one options from a range of pre-defined responses:<p>What is your favourite type of music?</p><input type="radio" name="genre" value="rock"/>Rock<input type="radio" name="genre" value="pop"/>Pop<input type="radio" name="genre" value="jazz"/>Jazz

Slide20

Form controls: radio buttons

Radio buttons allow the user to choose one options from a range of pre-defined responses:<p>What is your favourite type of music?</p><input type="radio" name="genre" value="rock"/>Rock<input type="radio" name="genre" value="pop"/>Pop<input type="radio" name="genre" value="jazz"/>JazzOnly one of the radio buttons can be selected at a time. If the user selects a new option, the previous one turns off.

Slide21

Form controls: radio buttons

Radio buttons allow the user to choose one options from a range of pre-defined responses:<p>What is your favourite type of music?</p><input type="radio" name="genre" value="rock"/>Rock<input type="radio" name="genre" value="pop"/>Pop<input type="radio" name="genre" value="jazz"/>JazzAll of the <input> elements that make up the group of radio buttons should have the same name. This is how the browser knows that the radio buttons go together as a group…usually the radio buttons in a group will be in close proximity to each other on the page, although nothing say this has to be the case!

Slide22

Form controls: radio buttons

Radio buttons allow the user to choose one options from a range of pre-defined responses:<p>What is your favourite type of music?</p><input type="radio" name="genre" value="rock"/>Rock<input type="radio" name="genre" value="pop"/>Pop<input type="radio" name="genre" value="jazz"/>JazzThe value of each radio button is what gets sent to the server if that particular option is selected when the form is submitted…this often closely matches the text (or "label") that accompanies the radio button, but doesn't have to

Slide23

Form controls: radio buttons

Radio buttons allow the user to choose one options from a range of pre-defined responses:<p>What is your favourite type of music?</p><input type="radio" name="genre" value="rock"/>Rock<input type="radio" name="genre" value="pop" checked="checked"/>Pop<input type="radio" name="genre" value="jazz"/>JazzYou can also use the checked attribute to specify which radio button should be the default…if you leave this off, it is possible for your user to leave none of the options selected (although in some cases you might want this)

Slide24

Form controls: checkboxes

Check boxes let the use give a positive or negative (yes or no) response to a single itemThere are two scenarios in which you might use checkboxes:If you want to have a one shot yes or no item:If you want to have a list of items from which the user can select one or more options

If you want to have a list of options which can have multiple things selected:

<p>What hobbies do you enjoy?</p>

<

input type="checkbox" name=

"hobbies[]"

value=

"stamps"

/>

Stamp collecting<

br

/>

<

input type="checkbox" name="

hobbies[]"

value=

"trains"

/>

Train spotting<

br

/

>

<

input type="checkbox" name="

hobbies[]"

value=

"

rimmerism

"

/>

Hammond organ music<

br

/>

<

input type="checkbox" name="

hobbies[]"

value=

"

paintdry

"

/>

Watching paint dry

Note the

name

attributes!

If you want the different options to go to the server within the same field, you must use the same

name

for all of them

If you use the same name on checkboxes and you expect to be able to read them server-side in PHP, you must append [] to the name

Slide27

Form controls: drop down lists

A drop down list lets the user select one

of a set of pre-defined options

It does not use the input element – instead, a surrounding select element contains a number of option elements:

<p>Who is usually credited with the invention of the World Wide Web?</p>

<select name="web">

<option value="

tim

">Tim Berners-Lee</option>

<option value="bill">Bill Gates</option>

<option value="

geoff

">Geoff Hurst in the 1966

World Cup</option>

</select>

Slide28

Form controls: drop down lists

A drop down list lets the user select oneof a set of pre-defined optionsIt does not use the input element – instead, a surrounding select element contains a number of option elements:<p>Who is usually credited with the invention of the World Wide Web?</p><select name="web"> <option value="tim">Tim Berners-Lee</option> <option value="bill">Bill Gates</option> <option value="geoff">Geoff Hurst in the 1966 World Cup</option></select>

The surrounding select element indicates that there is a drop down list

Slide29

Form controls: drop down lists

A drop down list lets the user select oneof a set of pre-defined optionsIt does not use the input element – instead, a surrounding select element contains a number of option elements:<p>Who is usually credited with the invention of the World Wide Web?</p><select name="web"> <option value="tim">Tim Berners-Lee</option> <option value="bill">Bill Gates</option> <option value="geoff">Geoff Hurst in the 1966 World Cup</option></select>

Each option element represents a different option that the user can select

Slide30

Form controls: drop down lists

A drop down list lets the user select oneof a set of pre-defined optionsIt does not use the input element – instead, a surrounding select element contains a number of option elements:<p>Who is usually credited with the invention of the World Wide Web?</p><select name="web"> <option value="tim" >Tim Berners-Lee</option> <option value="bill">Bill Gates</option> <option value="geoff">Geoff Hurst in the 1966 World Cup</option></select>

The

value

of an option is the text that will be sent to the server if the user chooses that option within their browser

Slide31

Form controls: drop down lists

A drop down list lets the user select oneof a set of pre-defined optionsIt does not use the input element – instead, a surrounding select element contains a number of option elements:<p>Who is usually credited with the invention of the World Wide Web?</p><select name="web" > <option value="tim">Tim Berners-Lee</option> <option value="bill">Bill Gates</option> <option value="geoff">Geoff Hurst in the 1966 World Cup</option></select>

The

name

of the select element contains the field name under which the results will be sent to the server.

Slide32

Form controls: drop down lists

<p>Who is usually credited with the invention of the World Wide Web?</p><select name="web"> <option value="tim">Tim Berners-Lee</option> <option value="bill">Bill Gates</option> <option value="geoff" selected="selected"> Geoff Hurst in the 1966 World Cup </option></select>

Slide33

Form controls: submit button

A submit button allows the user to send the form to the server

All the fields will be sent to the server, along with their values

Any server-side process will be then able to read these field/value pairs

The submit button is easy – a very basic

input

element with a type of

submit

<input type="submit"/>

You don't need to have a value or a name

If you don't have a value, then the browser will fill in something like "Submit Query" as the text on the

butto

..so if you want the button to say something sensible, put it in your

value

Slide34

An aside: Inline v block elements

The surrounding form element is an example of a block elementThis means that any surrounding content will always be separated from the form itselfThe quick brown fox jumped <form action="submit.php"> <input type="text" name="username"/></form>over the lazy dog's tail

Slide35

An aside: Inline v block elements

All of the elements within forms(e.g. input, select) are inlineelementsThis means that any surrounding content will flow around the element unless you use paragraphs, line breaks etc:<form action="submit.php"> The quick brown fox jumped <input type="text" name="username"/> over the lazy dog's tail</form>

Stage 1The end user navigates their browser to a page with a form on itThe browser requests the page from the serverThe server sends back a page of HTML (including the form markup)The browser renders the page (including the form)

HTML

Duckett

Slide38

The mechanics of form processing

Stage 2The end user fills in the form within their browser pageThe user clicks on the submit button on the formThe browser sends a request to the server for the URL that was in the action attribute of the form element As part of the request, the browser also sends the form dataOn the server, a server side process will examine the form data and generate a response in HTMLThis HTML is sent back to the browser and then rendered like any other page

HTML

Duckett

Form data

processdetails.php

<form

action="

processdetails.php

"

>

First name: <input name="

firstname

"/><

br

/>

Surname: <input name="surname"/><

br

/>

<input type="submit" value="Send Details"/>

</form>

Slide39

Handling forms from PHP

PHP has a special variable called $_REQUEST which is used for handling form inputYou can use this variable to read a specific form field thus:$surname = $_REQUEST["surname"] ;

get whatever the user typed into the

surname

field

Slide40

Handling forms from PHP

PHP has a special variable called $_REQUEST which is used for handling form inputYou can use this variable to read a specific form field thus:$surname = $_REQUEST["surname"];

put it into the new variable called

surname

Slide41

Handling forms from PHP

PHP has a special variable called $_REQUEST which is used for handling form inputYou can use this variable to read a specific form field thus:$banana = $_REQUEST["surname"];

put it into the new variable called

banana(you don't have to match any variable names in your PHP with your form field names – although often it makes good sense to do so!)

processdetails.php contains PHP code which is run server side. Within the code is a reference to the special $_REQUEST variable. Otherwise, it's just PHP code using the standard constructs you've all seen before…

Slide48

One crucial concept

Unlike programs you've written previously in languages like Banana, where you could mix input and processing, PHP has to handle user input in a batch

…so nothing stops you writing PHP that builds a form and the field elements within that form

Consider this specification:

A web application is needed that will generate three random numbers between 1 and 10. It will then ask the user to choose which number is their

favourite

. It should then display if their chosen number is more than five, equal to five or less than five.

Slide55

Building a form dynamically

Consider this specification:

A web application is needed that will generate three random numbers between 1 and 10. It will then

ask

the user to choose which number is their

favourite

. It should then

display if

their chosen number is more than five, equal to five or less than five.

How many individual HTML/PHP pages will there be? Which will be static (pure HTML) and which will be dynamic (includes PHP)?

Slide56

Building a form dynamically

Consider this specification:A web application is needed that will generate three random numbers between 1 and 10. It will then ask the user to choose which number is their favourite. It should then display if their chosen number is more than five, equal to five or less than five.How many individual HTML/PHP pages will there be? Which will be static (pure HTML) and which will be dynamic (includes PHP)?Break things down into processing and inputWhenever there is input, you can consider this the "end of a page"

Slide57

Building a form dynamically

Consider this specification:

A web application is needed that will

generate three random numbers between 1 and 10. It will then

ask

the user to choose which number is their

favourite

.

It should then

display if

their chosen number is more than five, equal to five or less than five

.

Our first page will be the part of the specification in

green

Our second page will be the part in

blue

What kind of pages will they be? Static HTML only, or will they need PHP code?

Slide58

Building a form dynamically

Page 1

A web application is needed that will

generate three random numbers between 1 and 10

. It will then ask the user to choose which number is their

favourite

.

Page 2

It

should then display

if their chosen number is more than five, equal to five or less than five

.

Both pages have dynamic elements (

highlighed

in

orange

)

So both pages will need to include PHP code

Slide59

Building a form dynamically

Page 1

Generate our random numbers

PHP code:

$num1 = rand(1,10);

$num2 = rand(1,10);

$num3 = rand(1,10);

Present the numbers in a form that allows the user to choose one of them

The way in which a user of a web app supplies input to the app is through a form

So, we will need to generate a form that includes the numbers and lets the user select one

Slide60

Building a form dynamically

Page 1

(continued) Present

the numbers in a form that allows the user to choose one of them

…we

will need to generate a form that includes the numbers and lets the user select

one

We could use radio buttons OR a select box

Slide61

Building a form dynamically

We need to write PHP

echo

statements that will generate HTML to create radio buttons

For each radio button, we will need HTML like

<input type="radio" name="

favenum

" value="1">

Number 1<

br

/>

If we were

echo

'ing

this HTML, we might

do something like

echo

'

<

input type="radio" name="

favenum

" value="1

">';

echo 'Number

1<

br

/

>';

Slide62

Building a form dynamically

We need to write PHP echo statements that will generate HTML to create radio buttonsFor each radio button, we will need HTML like<input type="radio" name="favenum" value="1">Number 1<br/>If we were echo'ing this HTML, we might do something likeecho ' <input type="radio" name="favenum" value="1"> ';echo 'Number 1<br/>';

why the single quotes rather than double quotes?

Slide63

Building a form dynamically

We need to write PHP

echo

statements that will generate HTML to create radio buttons

For each radio button, we will need HTML like

<input type="radio" name="

favenum

" value="

1

">

Number

1

<

br

/>

The grey text stays the same each time – only the numbers need to change

Our PHP will need to use string constants and concatenate our random number variables onto the text that stays the same

Slide64

Building a form dynamically

We need to write PHP

echo

statements that will generate HTML to create radio buttons

For each radio button, we will need HTML like

<input type="radio" name="

favenum

" value="

1

">

Number

1

<

br

/>

The grey text stays the same each time – only the numbers need to change

Our PHP will need to use string constants and concatenate our random number variables onto the text that stays the same

echo '<input type="radio" name="

favenum

" value="'.$num1.'"/>';

echo

'Number '.$num1."<

br

/>";

Slide65

Building a form dynamically

We need to write PHP echo statements that will generate HTML to create radio buttonsFor each radio button, we will need HTML like<input type="radio" name="favenum" value="1">Number 1<br/>The grey text stays the same each time – only the numbers need to changeOur PHP will need to use string constants and concatenate our random number variables onto the text that stays the sameecho ' <input type="radio" name="favenum" value=" '. $num1 .' "/> ';echo ' Number '. $num1 ." <br/> ";Parts highlighed with blue are constantsThose in red are variablesNote the use of the full stop symbol . for concatenation!

Slide66

Building a form dynamically

Page 1(continued) Present the numbers in a form that allows the user to choose one of them…we will need to generate a form that includes the numbers and lets the user select oneWe could use radio buttons OR a select box

<form action="

choose.php

">

<?

php

echo '<input type="radio" name="

favenum

" value="'.$num1.'"/>';

echo 'Number '.$num1."<

br

/>";

echo

'<input type="radio" name="

favenum

" value="'.$

num2.

'"/>';

echo 'Number '.$

num2.

"<

br

/>";

echo

'<input type="radio" name="

favenum

" value="'.$

num3.

'"/>';

echo 'Number '.$

num3.

"<

br

/>"

;

?>

<input type="submit"/>

</form>

Slide67

Building a form dynamically

Page 1(continued) Present the numbers in a form that allows the user to choose one of them…we will need to generate a form that includes the numbers and lets the user select oneWe could use radio buttons OR a select box

<form action="

choose.php

">

<select name="

favenum

">

<?

php

echo '<option value="'.$num1.'">Number '.$num1.'</option>';

echo '<option value="'.$

num2.

'">Number '.$

num2.

'</option>';

echo

'<option value="'.$

num3.

'">Number '.$

num3.

'</option>'

;

?>

</select>

<input type="submit"/>

</form>

Slide68

Building a form dynamically

Complete page (

index.php

)

<?

php

$num1 = rand(1,10); $num2

= rand(1,10)

; $num3

= rand(1,10)

;

?>

<html>

<head>

<title>Random number app</title>

</head>

<body>

<p>Choose your

favourite

random number:</p>

<form action="

choose.php

">

<?

php

echo '<input type="radio" name="

favenum

" value="'.$num1.'"/>';

echo 'Number '.$num1."<

br

/>";

echo '<input type="radio" name="

favenum

" value="'.$num2.'"/>';

echo 'Number '.$num2."<

br

/>";

echo '<input type="radio" name="

favenum

" value="'.$num3.'"/>';

echo 'Number '.$num3."<

br

/>";

?>

<input type="submit"/>

</

form

>

</body>

</html>

Slide69

Building a form dynamically

What the server might send to the browser, and what the browser would "see":

<html>

<head>

<title>Random number app</title>

</head>

<body>

<

p>Choose your

favourite

random number:</p>

<

form action="

choose.php

">

<

input type="radio" name="

favenum

" value="1">

Number

1<

br

/>

<

input type="radio" name="

favenum

" value=

"7"

>

Number 7<

br

/>

<

input type="radio" name="

favenum

" value=

"4"

>

Number 4<

br

/

>

<

input type="submit"/>

<

/form>

</body>

</html

>

Slide70

Building a form dynamically

This is now just an HTML form like any other

The browser doesn't care that the form fields were built dynamically in PHP – in fact it doesn't even know

The user can then interact with the form, supply data, make choices, in the normal way

When they click the

submit

button, the form data gets sent to the URL in the

action

attribute of the form element just as normal:

<form action="

choose.php

"

>

Slide71

Building a form dynamically

Consider this

specification (reminder)

A web application is needed that will generate three random numbers between 1 and 10. It will then ask the user to choose which number is their

favourite

.

It should then display if their chosen number is more than five, equal to five or less than five

.

The second highlighted part is now what we need to handle in

choice.php

Get the data that was submitted in the previous form in the input element named

favenum

e.g.

$

num

= $_REQUEST["

favenum

"];

Process the number and display the result

if ($

num

< 5)

{

echo "<p>Less than 5</p>";

}

else if ($

num

== 5)

{

echo "<p>Equal to 5</p>";

}

else

{

echo "<p>More than 5</p>";

}

Slide72

Building a form dynamically

Complete page (

choose.php

)

<html>

<head>

<title>Random number checker</title>

</head>

<body>

<p>Your random number is

<?

php

$

num

= $_REQUEST["

favenum

"];

if ($

num

< 5)

{

echo "Less

than

5";

}

else

if ($

num

== 5)

{

echo "Equal

to

5";

}

else

{

echo "More

than

5";

}

?>

</p>

</body>

</html>

Slide73

Double quotes, single quotes and "escaping"

This was a clunky line of code, wasn't it…?

echo '<input type="radio" name="

favenum

" value="'.$num1

.'"/>';

We used single quotes around the string literals in our echo statement so we could have double quotes within the string we were building

We could have flipped things round and had single quotes in our HTML, and used double quotes to delimit the string literals, i.e.

echo

"<

input

type='radio' name='

favenum

'

value

='".$

num1

."'/>";

Alternatively, we could have

escaped

our quotes

Slide74

Double quotes, single quotes and "escaping"

Certain symbols are called

escape characters

These symbols are those that are not easily represented within the rules that govern strings in a given programming language

Maybe they break delimiting rules – e.g. quotes

Maybe they are non-printable symbols which have other meaning in a given language – e.g. a carriage return

Escape characters are represented by an escape sequence when they need to appear in a string literal

Slide75

Double quotes, single quotes and "escaping"

Some useful escape sequences:

Sequence

Meaning

\"

Double quote

\t

Horizontal

tab

\\

Backslash

\$

Dollar

sign

Slide76

Double quotes, single quotes and "escaping"

So, if we wanted to delimit our previous echo statement with double quotes, and still have double quotes within our HTML that was being generated, we could do:

echo

"<input

type=\"radio\" name=\"

favenum

\"

value

=\"".$

num1

."\"/>";

Slide77

Double quotes, single quotes and "escaping"

So, if we wanted to delimit our previous echo statement with double quotes, and still have double quotes within our HTML that was being generated, we could do:echo "<input type= \" radio \" name= \" favenum \" value= \" ".$num1." \" />";

Slide78

Double quoted strings and variables

One of the advantages of using

double quotes to delimit our

strings is that you don't need to use concatenation to append or insert variable values

Unlike other programming languages, any variable names that are placed within a double quoted string literal will be expanded – the value of the variable will appear in the output

So, there is no need to do

echo "<input type=\"radio\" name=\"

favenum

\" value=\"".$num1."\"/>";

We could just do

echo "<input type=\"radio\" name=\"

favenum

\" value

=\"$num1\"/>";

Or even (if we were willing to accept single quotes in our HTML output)

echo "<input type='radio' name='

favenum

'

value='$num1'/>";

Slide79

Double quotes strings and variables

This is a PHP-specific thing!

Don't get in the habit of relying

on this – you can't do it in other languages

Consider

PHP:

echo "Hello there, $name";

Banana:

display "Hello there, name"

PHP variables are easily distinguished from other things because of their leading dollar sign

In most other languages, variables easily "blend in" to other text, so this sort of feature isn't possible

Slide80

Each page is an island…

One important concept to grasp is that each individual page has no "sight" of any previous pages

page1.php

<?

php

$

myNum

= rand(1,10);

echo "<p>My number was $

mynum

</p>"

?>

<a

href

="page2.php">Next page</a>

page2.php

<?

php

$echo "<p>The number was $

mynum

</p>";

?>

Slide81

Each page is an island…

One important concept to grasp is that each individual page has no "sight" of any previous pages

On an e-commerce site, it is common to display the contents of someone's basket prior to going ahead and placing an orderThe user will have placed items into their basket using a formAn intermediate screen displays all their items and asks them to confirmA final screen displays a thank you and confirmation messageAll this could be done using session variables:

Slide89

Summary

Web

forms are filled in by the user within their web browser

Each

field in a form usually has a name and a value

When

the user submits the form, the form data is sent to the server to be processed by a server-side process

Alternatively

, or additionally (or both!) the form data can be processed by client-side

Javascript

The

top level "block" of a form is the form element

Inside

the form element can be a variety of different input elements

A

submit button, when pressed, will send the form data to the server

Slide90

Summary

Any and all user input in PHP we need to get through an HTML form

This means that we need to process user input in batches – we can't mix processing and input

This means your PHP "program" – or more accurately, your website – may be split across two or more files

We can read form fields using the $_REQUEST variable

Nothing stops us generating a form dynamically – in fact for things like pull down lists and radio buttons we might have to do that

Slide91

Summary

HTTP is stateless – every individual request is its own thing and has no "sight" of any other

There are ways around this – in PHP we have the session API

Use the $_SESSION variable to put items into the session

These variables persist across pages for the duration of the user's browser session on