Tải bản đầy đủ (.pdf) (85 trang)

Beginning PHP 5.3 phần 4 pdf

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (954.14 KB, 85 trang )

Part II: Learning the Language

Exercises
1.

Write a Calculator class that can store two values, then add them, subtract them, multiply
them together, or divide them on request. For example:

$calc = new Calculator( 3, 4 );
echo $calc->add(); // Displays “7”
echo $calc->multiply(); // Displays “12”

2.

Create another class, CalcAdvanced, that extends (inherits from) the Calculator class.
CalcAdvanced should be capable of storing either one or two values:

$ca = new CalcAdvanced( 3 );
$ca = new CalcAdvanced( 3, 4 );
CalcAdvanced should also add the following methods:



pow() that returns the result of raising the first number (the base) to the power of the

second number


sqrt() that returns the square root of the first number




exp() that returns e raised to the power of the first number

(Hint: PHP contains built-in functions called pow(), sqrt(), and exp().)

218


Part III

Using PHP in Practice
Chapter 9: Handling HTML Forms with PHP
Chapter 10: Preserving State With Query Strings, Cookies
and Sessions
Chapter 11: Working with Files and Directories
Chapter 12: Introducing Databases and SQL
Chapter 13: Retrieving Data from MySQL with PHP
Chapter 14: Manipulating MySQL Data with PHP
Chapter 15: Making Your Job Easier with PEAR
Chapter 16: PHP and the Outside World
Chapter 17: Generating Images with PHP
Chapter 18: String Matching with Regular Expressions
Chapter 19: Working with XML
Chapter 20: Writing High-Quality Code



9
Handling HTML Forms
with PHP

You’ve now learned the basics of PHP. You know how PHP scripts work, and you’ve studied the
important building blocks of the language, including variables, operators, decisions, looping,
strings, arrays, functions, and objects.
Now it’s time to start building real-world applications with PHP, and a key part of most PHP
applications is the ability to accept input from the person using the application. So far, all the
scripts you’ve created haven’t allowed for any user input at all; to run the script, you merely type
its URL into your Web browser and watch it do its stuff. By adding the ability to prompt the user
for input and then read that input, you start to make your PHP scripts truly interactive.
One of the most common ways to receive input from the user of a Web application is via an HTML
form. You’ve probably filled in many HTML forms yourself. Common examples include contact
forms that let you email a site owner; order forms that let you order products from an online store;
and Web-based email systems that let you send and receive email messages using your Web
browser.
In this chapter, you learn how to build interactive Web forms with PHP. You look at:


Creating HTML forms



Writing PHP scripts to capture the data sent from your forms



Some of the security issues surrounding form data



How to handle empty form fields, as well as form fields that send more than one value
at once




Using PHP scripts to generate Web forms, giving your forms a lot of flexibility



Creating forms with built-in error checking


Part III: Using PHP in Practice


How to use hidden form fields to create a user-friendly three-stage registration form



Creating forms that allow users to upload files



How to use page redirection to make your forms smoother and safer to use

Once you’ve worked through this chapter you’ll be able to use Web forms to make your PHP scripts
much more useful and flexible.

How HTML Forms Work
Before looking at the PHP side of things, take a quick look at how an HTML form is constructed. (If
you’re already familiar with building HTML forms you may want to skip this section.)
An HTML form, or Web form, is simply a collection of HTML elements embedded within a standard

Web page. By adding different types of elements, you can create different form fields, such as text fields,
pull-down menus, checkboxes, and so on.
All Web forms start with an opening <form> tag, and end with a closing </form> tag:
<form action=”myscript.php” method=”post”>
<!-- Contents of the form go here -->
</form>

By the way, the second line of code in this example is an HTML comment –– everything between the
<!-- and --> is ignored by the Web browser.
Notice that there are two attributes within the opening <form> tag:


action tells the Web browser where to send the form data when the user fills out and
submits the form. This should either be an absolute URL (such as />myscript.php) or a relative URL (such as myscript.php, /myscript.php, or ../
scripts/myscript.php). The script at the specified URL should be capable of accepting
and processing the form data; more on this in a moment.



method tells the browser how to send the form data. You can use two methods: get is useful for
sending small amounts of data and makes it easy for the user to resubmit the form, and post
can send much larger amounts of form data.

Once you’ve created your basic form element, you can fill it with various elements to create the fields
and other controls within your form (as well as other HTML elements such as headings, paragraphs, and
tables, if you so desire).

222



Chapter 9: Handling HTML Forms with PHP
Try It Out

Create an HTML Form

In this example, you create a Web form that contains a variety of form fields. Not only will you learn
how to create the various types of form fields, but you can see how the fields look and work in your
Web browser.
Save the following file as web_form.html in your document root folder, then open it in your browser
to see the form:
“ /><html xmlns=” xml:lang=”en” lang=”en”>
<head>
<title>An HTML Form</title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
</head>
<body>

An HTML Form


<form action=”” method=”get”>
<div style=”width: 25em;”>
<label for=”textField”>A text input field</label>
<input type=”text” name=”textField” id=”textField” value=”” />
<label for=”passwordField”>A password field</label>
value=”” />
<label for=”checkboxField”>A checkbox field</label>
value=”yes” />
<label for=”radioButtonField1”>A radio button field</label>

value=”radio1” />
<label for=”radioButtonField2”>Another radio button</label>
value=”radio2” />
<label for=”submitButton”>A submit button</label>
value=”Submit Form” />
<label for=”resetButton”>A reset button</label>
value=”Reset Form” />
<label for=”fileSelectField”>A file select field</label>
value=”” />

223


Part III: Using PHP in Practice
<label for=”hiddenField”>A hidden field</label>
<input type=”hidden” name=”hiddenField” id=”hiddenField” value=”” />
<label for=”imageField”>An image field</label>
src=”asterisk.gif” width=”23” height=”23” />
<label for=”pushButton”>A push button</label>
value=”Click Me” />
<label for=”pullDownMenu”>A pull-down menu</label>
<select name=”pullDownMenu” id=”pullDownMenu” size=”1”>
<option value=”option1”>Option 1</option>
<option value=”option2”>Option 2</option>

<option value=”option3”>Option 3</option>
</select>
<label for=”listBox”>A list box</label>
<select name=”listBox” id=”listBox” size=”3”>
<option value=”option1”>Option 1</option>
<option value=”option2”>Option 2</option>
<option value=”option3”>Option 3</option>
</select>
<label for=”multiListBox”>A multi-select list box</label>