0% found this document useful (0 votes)
2 views19 pages

Chapter 4 Form

HTML forms are essential for sending data to PHP scripts, which can read input from various form elements and dynamically create HTML pages. The document explains the methods of form submission (GET and POST), the use of superglobal arrays in PHP to store form data, and the importance of data validation. It also covers form elements like text fields, radio buttons, checkboxes, and hidden fields, along with tips for accessing and validating user input.

Uploaded by

hirpaadugna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Chapter 4 Form

HTML forms are essential for sending data to PHP scripts, which can read input from various form elements and dynamically create HTML pages. The document explains the methods of form submission (GET and POST), the use of superglobal arrays in PHP to store form data, and the importance of data validation. It also covers form elements like text fields, radio buttons, checkboxes, and hidden fields, along with tips for accessing and validating user input.

Uploaded by

hirpaadugna1
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

HTML Forms

• HTML forms are an important way to send data


to PHP scripts
• PHP scripts can read the input from any type of
HTML form element.
• The form data is available in the PHP script
• Can be used to dynamically create HTML pages
• Can be used in conjunction with a database

PHP Workshop ›#‹


How forms work
User requests a particular URL

XHTML Page supplied with Form

User fills in form and submits.


Another URL is requested and the
Form data is sent to this page either in
URL or as a separate piece of data.
User
Web Server
XHTML Response

PHP Workshop ›#‹


Forms method

• method specifies how the form sends data


to the script - post or get
• action specifies which page the form data
will be sent to when the submit button is
clicked
• Our buttons will now use type="submit” to
submit the form data to the php script
PHP Workshop ›#‹
The get Method
• The get method embeds data in the URL
• All form data is passed as a query string
• All the data is listed after a ‘?’ with ‘&’
separating the different elements
• Spaces are replaced with a ‘+’
• [Link]
[Link]?userName=Aileen+Pierce
• Data is visible in the URL and not secure
• Easy to bookmark
PHP Workshop ›#‹
The post Method
• post is the standard method for submitting
data stored in a form to Web server.
• Secure because the data is not visible in
the URL.
• We will mainly be using the post method
• PHP scripts store the data it receives from
HTML forms in special variables called
super global arrays.

PHP Workshop ›#‹


Superglobal Arrays
• PHP has built-in variables called superglobal
arrays that store data. These are associative
arrays
• Data sent to the script with the get method is
saved in the $_GET[] superglobal array
• Data sent to the script with the post method is
saved in the $_POST[] superglobal array
• $_REQUEST[] stores data sent to the script with
either method.

PHP Workshop ›#‹


Text Fields

• $_POST[‘address’] is automatically created by php


• $_POST[‘address’] will have the value entered in the text
field.
Radio Buttons

• If the radio button is selected PHP automatically creates


$_POST[‘contact’] = "Yes" ¤ If a radio button is not
checked, an array element is not created.

PHP Workshop ›#‹


Check Boxes

• If the check box is checked PHP


automatically creates $_POST[‘music’] =
"Rock"
• If a check box is not checked, an array
element is not created.

PHP Workshop ›#‹


Validation
• In the case of radio buttons and check
boxes, if they are not chosen, no element
gets added to the array.
• Use the isset() function to determine if a
check box or radio button has been
chosen
✓ Returns true if it exists
✓ Returns false if it does not exist

PHP Workshop ›#‹


List Boxes

• The name of the select object becomes


the name of the array element.
• The value of whichever option the user
selected is assigned as the value of that
array element. If Coke is chosen,
$_POST[‘soda’]= "Coke"
PHP Workshop ›#‹
PHP Errors
• If the PHP script shows blank spaces
where a value should be it means the
variable has no value.
• Check spelling and case of HTML form
element name and the variable name
• Completely blank page means there’ s a
problem your PHP script
• Double check your syntax

PHP Workshop ›#‹


Lab
Add radio buttons, check boxes, and a list
box to your form and have PHP print out
the values chosen in the form.
Make sure all your form elements are
within the <form> tag so their values are
sent to the PHP script.

PHP Workshop ›#‹


Form tags
• 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).
PHP Workshop ›#‹
Hidden Fields
<input type="hidden"
name="hidden_value"
value="My Hidden Value" />

• name=“…” is the name of the field.


• value=“…” is the actual data sent back
to PHP.

PHP Workshop ›#‹


In PHP…
• The form variables are available to PHP in
the page to which they have been
submitted.
• The variables are available in two super
global arrays created by PHP called
$_POST and $_GET.

PHP Workshop ›#‹


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’];

PHP Workshop ›#‹


A warning..
NEVER TRUST USER INPUT

• Always check what has been input.


• Validation can be undertaken using
Regular expressions or in-built PHP
functions.
A useful tip..
• I find that storing the validated data in a
different array to the original useful.
• I often name this array ‘clean’ or
something similarly intuitive.
• I then *only* work with the data in $clean,
and never refer to $_POST/$_GET again.

PHP Workshop ›#‹


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
}

PHP Workshop ›#‹

You might also like