0% found this document useful (0 votes)
40 views24 pages

Chapter 02-2.1-How To Work With Form Data

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)
40 views24 pages

Chapter 02-2.1-How To Work With Form Data

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
You are on page 1/ 24

Chapter 2 (2.

1)
How to work
with form data

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 1
Objectives
Applied
1. Use text boxes, password boxes, radio buttons, check boxes,
drop-down lists, list boxes, and text areas to get input from the
user.
2. Use hidden fields to pass data to the web application when a form
is submitted.
3. Use the htmlspecialchars and nl2br functions to display user
entries the way you want them displayed.
4. Use echo statements to display data in a web page.

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 2
Objectives (continued)
Knowledge
1. Describe the way a PHP application gets data from text boxes,
password boxes, hidden fields, radio buttons, check boxes, drop-
down lists, list boxes, and text areas.
2. Describe the use of the htmlspecialchars and nl2br functions.
3. Describe the use of the echo and print statements.

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 3
Text input: the HTML for three types of fields
<input type="text" name="user_name" value="rharris">
<input type="password" name="password">
<input type="hidden" name="action" value="login">

The text and password fields in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 4
The URL when using the GET method
process_data.php?user_name=rharris&password=s3cr3t72&action=login

The PHP for the GET method


<?php
$user_name = filter_input(INPUT_GET, 'user_name');
$password = filter_input(INPUT_GET, 'password');
$action = filter_input(INPUT_GET, 'action');
?>

The URL when using the POST method


process_data.php

The PHP for the POST method


<?php
$user_name = filter_input(INPUT_POST, 'user_name');
$password = filter_input(INPUT_POST, 'password');
$action = filter_input(INPUT_POST, 'action');
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 5
The HTML for three radio buttons in a group
<input type="radio" name="card_type" value="visa" checked>
Visa<br>
<input type="radio" name="card_type" value="mastercard">
MasterCard<br>
<input type="radio" name="card_type" value="discover">
Discover

The radio buttons in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 6
PHP to access a radio button group
<?php
$card_type = filter_input(INPUT_POST, 'card_type');
?>

PHP to add a default value for a group


with no default button
<?php
$card_type = filter_input(INPUT_POST, 'card_type');
if ($card_type == NULL) {
$card_type = 'unknown';
}
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 7
The HTML for three check boxes
<input type="checkbox" name="pep" checked> Pepperoni<br>
<input type="checkbox" name="msh"> Mushrooms<br>
<input type="checkbox" name="olv"> Olives

The check boxes in the browser

The PHP to access the check box data


<?php
$pepperoni = isset($_POST['pep']);
$mushrooms = isset($_POST['msh']);
$olives = isset($_POST['olv']);
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 8
Three related check boxes in an array
<input type="checkbox" name="top[]" value="pep"> Pepperoni<br>
<input type="checkbox" name="top[]" value="msh"> Mushrooms<br>
<input type="checkbox" name="top[]" value="olv"> Olives

The check boxes in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 9
PHP that accesses the array and its values
<?php
$toppings = filter_input(INPUT_POST, 'top',
FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
if ($toppings !== NULL) {
$top1 = $toppings[0]; // $top1 is pep
$top2 = $toppings[1]; // $top2 is olv
$top3 = $toppings[2]; // $top3 is not set – error
}
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 10
PHP that uses a loop to process the array
<?php
$toppings = filter_input(INPUT_POST, 'top',
FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
if ($toppings !== NULL) {
foreach($toppings as $key => $value) {
echo $key. ' = ' . $value . '<br>';
}
} else {
echo 'No toppings selected.';
}
?>

The message displayed by the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 11
The HTML for a drop-down list
<select name="card_type">
<option value="visa">Visa</option>
<option value="mastercard">MasterCard</option>
<option value="discover">Discover</option>
</select>

The drop-down list in a browser

The HTML to set a default option


<option value="mastercard" selected>MasterCard</option>

The PHP to access the drop-down list data


<?php
$card_type = filter_input(INPUT_POST, 'card_type');
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 12
A list box that doesn’t allow multiple selections
<select name="card_type" size="3">
<option value="visa">Visa</option>
<option value="mastercard">MasterCard</option>
<option value="discover">Discover</option>
</select>

The list box in a browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 13
A list box that allows multiple selections
<select name="top[]" size="3" multiple>
<option value="pep" selected>Pepperoni</option>
<option value="msh">Mushrooms</option>
<option value="olv">Olives</option>
</select>

The list box in a browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 14
PHP for a list box that allows multiple selections
<?php
$toppings = filter_input(INPUT_POST, 'top',
FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
if ($toppings !== NULL) {
foreach ($toppings as $key => $value) {
echo $key. ' = ' . $value . '<br>'; // 0 = pep, 1 = msh
}
} else {
echo 'No toppings selected.';
}
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 15
The HTML for a text area
<textarea name="comment" rows="4" cols="50">Welcome
to PHP and MySQL!</textarea>

A text area in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 16
The URL when using the GET method
When the user includes spaces in the text area
process_data.php?comment=Welcome+to+PHP+and+MySQL!
When the user presses the Enter or Return key
process_data.php?comment=Welcome+to%0D%0APHP+and+MySQL!
When the user doesn’t enter any text
process_data.php?comment=

The PHP to get the data from the text area


<?php
$comment = filter_input(INPUT_POST, 'comment');
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 17
Syntax of the htmlspecialchars() function
htmlspecialchars($string[, $quote_style[, $charset[,
$double_encode]]])

Common HTML character entities


Character Character entity
& &amp;
< &lt;
> &gt;
" &quot;
' &#039;
Non-breaking space &nbsp;

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 18
An example that uses special characters
The text entered by the user

PHP that converts special characters to character entities


<?php
$comment = $_POST['comment'];
$comment = htmlspecialchars($comment);
?>
<p><?php echo $comment; ?></p>
The data displayed in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 19
A double-encoded less than character entity (&lt;)
&amp;lt;

A statement that prevents double encoding


$comment = htmlspecialchars($comment, ENT_COMPAT,
'ISO-8859-1', false);

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 20
Syntax of the nl2br() function
nl2br($string[, $is_xhtml])

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 21
The text entered into the text area

Converting line break characters


to line break tags
<?php
$comment = filter_input(INPUT_POST, 'comment');
$comment = nl2br($comment, false); // use <br> tags,
not <br /> tags
?>
<p><?php echo $comment; ?></p>

The data displayed in the browser

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 22
The echo statement
Syntax
echo $var1
echo($var1)
echo $var1 [, $var2 ...]

Examples
echo 'Welcome to PHP and MySQL!';
echo 'Name: ' . $name;
echo('Name: ' . $name);
echo 'Cost: $', $cost;

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 23
The print statement
Syntax
echo $var1
echo($var1)
echo $var1 [, $var2 ...]
Examples
print 'Welcome to PHP and MySQL!';
print 'Name: ' . $name;
print('Name: ' . $name);
Using print in an expression
<?php
($age >= 18) ? print('Can vote.') : print('Cannot vote.');
?>

© 2017, Mike Murach & Associates, Inc.


Murach's PHP and MySQL (3rd Ed.) C7, Slide 24

You might also like