Unit 2KD
Unit 2KD
1
UNIT COVERS
2
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
Web Pages:
Web pages are delivered by a web server to the user and displayed in a web browser.
A website consists of many web pages linked together under a common domain name.
Each webpage is linked with a unique URL; hence two pages cannot have the same URL.
PHP is used to create webpages that can interact with databases, process user input, and generate dynamic
content.
3
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
A static website means that you’ll see the same content every time.
Dynamic website changes its content and adjust to its users through various forms of interaction.
For example, you can purchase a product or write a message – all possible due to dynamic websites.
4
All websites consist of a certain part of HTML code.
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
5
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
6
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
7
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
8
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
9
HOW DOES PHP WORK WITH THE WEB SERVER AND BROWSER?
HOW DOES PHP WORK WITH THE WEB SERVER AND BROWSER?
Step 1 The user enters `http://laravel.com` into their browser and taps/hits 'enter'.
Step 2 After the user has tapped/hit 'enter', the browser sends the page request over the Internet to the web server.
Step 3 The web server gets the request and analyzes the request information. Apache realizes that we didn't specify a
file, so it looks for a directory index and finds `index.php`.
Step 4 Since Apache knows to send files that end with the `.php` file extension to the PHP interpreter, it asks PHP to
execute the file.
Step 5 In this step, PHP is executing the code contained in the `index.php` file from the request. During this step, PHP
may interact with databases, the file system or make external API calls, amongst other things.
Step 6 After PHP has finished executing the `index.php` file, it sends the output back to Apache.
Step 7 Apache receives the output from PHP and sends it back over the Internet to a user's web browser. This is called
the `web response`.
Step 8 The user's web browser receives the response from the server, and renders the web page on a computer or 10
device.
2.1 INTRODUCTION AND NEEDS - WEB PAGES TO COMMUNICATE WITH PHP
Form:
An HTML form is used to collect user input.The user input is most often sent to a server for
processing.
The Form is an HTML element that is used to create various forms like – Entry form, Signup
form, login form, etc.
11
GET AND POST METHODS
There are two ways the browser client can send information to the web server.
The GET Method
The POST Method
GET and POST are the methods to transfer data from one page to another page or on the same page but from
HTML form to PHP (or any other server-side language)
12
GET METHOD
GET: It is a method in which data is passed through the url (as variables) which is visible in the addressbar
of web-browser.
13
GET METHOD
The GET method sends the encoded user information appended to the page request. The page and the encoded
information are separated by the ?character.
ü The GET method produces a long string that appears in your server logs, in the browser's Location: box.
ü Never use GET method if you have password or other sensitive information to be sent to the server.
ü GET can't be used to send binary data, like images or word documents, to the server.
ü The data sent by GET method can be accessed using QUERY_STRING environment variable.
ü The PHP provides $_GET associative array to access all the sent information using GET method.
14
POST METHOD
POST: It is a method in which data is passed though packets which are not visible to the user on web-browser.
So, it is much safer to use POST instead of GET.
<variable> = $_POST["<name of variable>"];
15
POST METHOD
The information is encoded as described in case of GET method and put into a header called
QUERY_STRING.
The POST method does not have any restriction on data size to be sent.
The POST method can be used to send ASCII as well as binary data.
The data sent by POST method goes through HTTP header so security depends on HTTP protocol.
By using Secure HTTP you can make sure that your information is secure.
16
The PHP provides $_POST associative array to access all the sent information using POST method.
ACCESS DATA
Access submitted data in the relevant array for the submission type, using the
input name as a key.
<form action=“path/to/submit/page”
method=“get”>
<input type=“text” name=“email”>
</form>
$email = $_GET[‘email’];
17
DIFFERENCE BETWEEN GET AND POST
GET:
We need to know..
19
HOW FORMS WORK
20
XHTML FORM
<form action=“path/to/submit/page”
method=“get”>
<!–- form contents -->
</form>
21
FORM TAGS
action=“…” is the page that the form should submit its data to.
method=“…” is the method by which the form data is submitted. The option are either get or
post. If the method is get the data is passed in the url string, if the method is post it is passed as a
separate file.
22
INPUT
INPUT attributes
ü type: the kind of user input control
ü name: the name of the control
This gets passed through to the handling code
In PHP: $_POST[‘name’]
ü value: initial value of the control
ü size: initial width of the control
in pixels, except for text and password controls
ü maxlength: for text/password, maximum number of characters allowed
ü checked: for radio/checkbox, specifies that button is on
ü src: for image types, specifies location of image used to decorate input button
23
SPECIAL BUTTONS
File upload
file: creates a file upload control
24
INPUT CONTROL TYPES
text:
password:
checkbox:
radio:
button
hidden:
25
FORM FIELDS: TEXT INPUT
Use a text input within form tags for a single line freeform text input.
name=“…” is the name of the field.You will use this name in PHP to access the data.
id=“…” is label reference string – this should be the same as that referenced in the
<label> tag.
size=“…” is the length of the displayed text box (number of characters).
27
FORM FIELDS: PASSWORD INPUT
<label for=“pw">Password</label>
<input type=“password"
name=“passwd"
id=“pw"
size="20"/>
28
FORM FIELDS: TEXT INPUT
<label for="desc">Description</label>
<textarea name=“description”
id=“desc“
rows=“10” cols=“30”>
Default text goes here…
</textarea>
29
FORM FIELDS: TEXT AREA
name=“…” is the name of the field.You will use this name in PHP to access the data.
id=“…” is label reference string – this should be the same as that referenced in the
<label> tag.
rows=“…” cols=“..” is the size of the displayed text box.
30
FORM FIELDS: DROP DOWN
31
FORM FIELDS: DROP DOWN
32
FORM FIELDS: RADIO BUTTONS
name=“…” is the name of the field. All radio boxes with the same name are grouped
with only one selectable at a time.
id=“…” is label reference string.
value=“…” is the actual data sent back to PHP if the option is selected.
checked=“checked” this option is selected by default.
34
FORM FIELDS: CHECK BOXES
36
HIDDEN FIELDS
<input type="hidden"
name="hidden_value"
value="My Hidden Value" />
37
SUBMIT BUTTON..
A submit button for the form can be created with the code:
<input type="submit"
name="submit"
value="Submit" />
38
FIELDSET
In XHTML 1.0, all inputs must be grouped within the form into fieldsets. These represent logical
divisions through larger forms. For short forms, all inputs are contained in a single fieldset.
<form>
<fieldset>
<input … />
<input … />
</fieldset>
<fieldset>
<input … />
<input … />
</fieldset>
</form>
39
A WARNING..
40
IS IT SUBMITTED?
We also need to check before accessing data to see if the data is submitted, use isset() function.
if (isset($_POST[‘username’])) {
// perform validation
}
41
VALIDATION
43