Chapter 07
Chapter 07
Chapter 07
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. 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.
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. 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.
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. 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"/>
Hidden fields:
• not displayed on the web page
• value attribute must be set
• not secure since the value can be viewed in the html
source code
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 4
The URL when using the GET method
process_data.php?user_name=rharris&password=s3cr3t72&action=login
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 5
The HTML for three radio buttons in a group
<input type="radio" name="card_type" value="visa"
checked="checked"/> Visa<br />
<input type="radio" name="card_type" value="mastercard"/>
MasterCard<br />
<input type="radio" name="card_type" value="discover"/>
Discover
Note:
• the name attribute for each radio button must be the
same
• the value attribute for each radio button must be
different
• the checked attribute sets the default so should only
be used on one of the options
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 6
PHP to access a radio button group
with a default button
<?php
$card_type = $_POST['card_type'];
?>
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 7
The HTML for three check boxes
<input type="checkbox" name="pep" checked="checked"/>
Pepperoni<br />
<input type="checkbox" name="msh"/> Mushrooms<br />
<input type="checkbox" name="olv"/> Olives
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 8
Using an array of check boxes
• If the items in a checkbox list are related, use an array to simplify the
processing rather than creating separate identifiers for each one.
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
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 9
PHP that uses a loop to process the array
<?php
if (isset($_POST['top'])) {
$toppings = $_POST['top'];
foreach($toppings as $key => $value) {
echo $key. ' = ' . $value . '<br />';
}
} else {
echo 'No toppings selected.';
}
?>
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 10
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>
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 11
A list box that doesn’t allow multiple options
<select name="card_type" size="3">
<option value="visa">Visa</option>
<option value="mastercard">MasterCard</option>
<option value="discover">Discover</option>
</select>
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 12
A list box that allows multiple options
<select name="top[]" size="3" multiple="multiple">
<option value="pep"
selected="selected">Pepperoni</option>
<option value="msh">Mushrooms</option>
<option value="olv">Olives</option>
</select>
If the user types past the end of the text area, a soft return is
created
If the user presses Enter or Return, a hard return is created.
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 14
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!
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 15
htmlspecialchars() converts certain characters into their
character entity so that they will show in the browser and
not be interpreted.
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 16
Syntax of the htmlspecialchars function
htmlspecialchars($string[, $quote_style[, $charset
[, $double_encode]]])
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 17
Converting special characters to entities
<?php
$comment = $_POST['comment'];
$comment = htmlspecialchars($comment, ENT_COMPAT,
'ISO-8859-1', false);
?>
<p><?php echo $comment; ?></p>
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 18
Syntax of the nl2br function
nl2br($string[, $is_xhtml])
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 19
The text entered into the text area
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 20
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;
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 21
The print statement
Syntax
print $var1
print($var1)
Examples
print 'Welcome to PHP and MySQL!';
print 'Name: ' . $name;
print('Name: ' . $name);
Murach's PHP and MySQL, C7 © 2010, Mike Murach & Associates, Inc. Slide 22