1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P54 doc

5 189 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 115,21 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

You might be surprised that I don’t have special methods to begin and end the form.. If you try to map the select and option combination to a PHP structure, the most obvious comparison i

Trang 1

$numArray = array(

“1”=>”ichii”,

“2”=>”nii”,

“3”=>”san”,

“4”=>”shi”

);

$s->select(“options”, $numArray);

$s->h3(“make form elements inside a table!”);

$myArray = array(

array($s->gTag(“b”,”name”), $s->gTextbox(“name”)),

array($s->gAddText(“address”), $s->gTextbox(“address”)),

array($s->gAddText(“phone”), $s->gTextbox(“phone”)),

array($s->gAddText(“favorite number”), $s->gSelect(“number”, $numArray))

);

$s->buildTable($myArray);

$s->submit();

$s->addText(“</form> \n”);

$s->h3(“results from previous form (if any)”);

$s->formResults();

$s->buildBottom();

print $s->getPage();

?>

Building a Simple Form and Adding a Text Box

The following code snippet builds the most basic SuperHTMLform:

$s->addText(“<form>”);

$s->textbox(“userName”, “Joe”);

$s->submit();

$s->addText(“</form>”);

I used the addText() method to provide the basic form tags and then created a

textbox and submit button using the SuperHTMLobject’s special methods

243

j e

Trang 2

You might be surprised that I don’t have special methods to begin and end the form They would be easy, but I felt they wouldn’t simplify things much, so I just used the

addText() method to add form tags (Of course you are free to add these methods

yourself if you wish The SuperHTML project is designed as a framework only, and I’m eager to see people add new functionality to it.)

The textbox()method can take one or two parameters The first parameter is the name of the resulting <input>element The second (optional) parameter is the ele-ment’s default value If you do not specify a value, it is left blank

Of course, the submit button resolves to almost the same kind of HTML code, and

it works very much like the textbox However, if you leave off the submitmethod’s second parameter, your HTML code will show the typical Submit Querycaption

Building Drop-Down Menus

There are a number of times you’ll want the user to choose input from a limited number of options Often, the value you want to send to the next program isn’t exactly what the user sees The appropriate HTML device for this situation is the

<select>element with a bunch of <option>objects inside If you try to map the select and option combination to a PHP structure, the most obvious comparison is

an associative array as you used in chapter 5, “Better Arrays and String Handling.” Take a look at the following code fragment to see how this works

$numArray = array(

“1”=>”ichii”,

“2”=>”nii”,

“3”=>”san”,

“4”=>”shi”

);

$s->select(“options”, $numArray);

I created an associative array using numbers as indices and the Japanese names for the numerals as the values

Once I had created the array, it was easy to create a select object with the cleverly named select()method The two parameters are the name of the resulting select object and the array This code produces the following HTML:

<select name = “options” >

<option value = “1”>ichii</option>

<option value = “2”>nii</option>

244

g r

s o

l u

g in

e r

Trang 3

<option value = “3”>san</option>

<option value = “4”>shi</option>

</select>

Building Form Elements Inside a Table

It’s important to have professional-looking documents Most programmers place

all form elements inside a table The SuperHTMLobject makes this relatively easy

to do, using the buildTable()method described previously in this chapter

How-ever, there’s one new twist Take a look at the code and see if you can spot it

$myArray = array(

array($s->gTag(“b”,”name”), $s->gTextbox(“name”)),

array(“address”, $s->gTextbox(“address”)),

array(“phone”, $s->gTextbox(“phone”)),

array(“favorite number”, $s->gSelect(“number”, $numArray))

);

$s->buildTable($myArray);

The $myArrayvariable is a big array that controls the eventual table Each row

con-sists of two columns The first column is a label specifying the type of data being

collected; the second column is some sort of form element

Here’s the twist: Although the methods used inside the array look familiar, every

single one is new! Recall that all the methods for building a page in SuperHTMLwork

by adding content directly to some variable in memory This is done to make the

program more flexible, so it can be used both to print a result and save it to a file

When I’m building the array, I don’t want to actually add anything to the HTML

document Instead, I want to receive the results of the function so I can add it to

my array I then add the entire array to the page using the buildTable()method

Most SuperHTMLmethods have a getvariant, preceded with g For example, recall

that $s->tag(“b”,“name”)produces the code <b>name</b>and immediately adds

that code to the internal HTML document The gvariant of the same command

(following) produces exactly the same code but does not add it to the internal

representation:

$s->gTag(“b”, “name”);

Instead, it returns the value so you can do whatever you want with it In this case,

I simply want to add the code to my table as a pseudo-heading (In fact I could do

any kind of HTML here, including Cascading Style Sheets magic to get exactly the

look I want.)

245

j e

Trang 4

All other elements in the table are either plain text (the other labels) or other calls to getversions of methods in the SuperHTMLobject After describing all the information in a two-dimension array, it’s very easy to add it to the internal doc-ument using the buildTable()method

Although it may seem tedious to build a two-dimension array, consider the com-plexity of the output that is produced by this function:

<table border = 1>

<tr>

<td><b>

name

</b>

</td>

<td><input type = “text”

name = “name”

value = “” />

</td>

</tr>

<tr>

<td>address</td>

<td><input type = “text”

name = “address”

value = “” />

</td>

</tr>

<tr>

<td>phone</td>

<td><input type = “text”

name = “phone”

value = “” />

</td>

</tr>

<tr>

<td>favorite number</td>

<td><select name = “number” >

<option value = “1”>ichii</option>

<option value = “2”>nii</option>

<option value = “3”>san</option>

<option value = “4”>shi</option> </select>

</td>

246

g r

s o

l u

g in

e r

Trang 5

</tr>

</table>

I think the version written with SuperHTMLis quite a bit easier to understand and

maintain I like that most of the details are hidden away in the object definition

Viewing Form Results

Usually when I build a PHP page that responds to a form, I begin by retrieving the

names and values of all the fields that come from the form This is useful because

often I make mistakes in my field names or forget exactly what I’m expecting the

form to send It would be nice to have a really easy way to do this Of course,

SuperHTMLhas this feature built in If you fill in the form elements in Forms.php

and click the submitbutton, you get another version of Forms.php, but this one

also includes form data, as shown in Figure 7.7

The code that produces these results is quite simple:

$s->formResults();

If there was no previous form, formResults()returns an empty string If the page

has been called from a form, the resulting code looks something like this:

<table border = “1”>

<tr>

<td>userName</td>

247

j e

FIGURE 7.7

After submitting

Forms.php, the

second call to

Forms.php returns a

table of field names

and values.

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN