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

PHP 5/MySQL Programming- P12 pps

5 272 0

Đ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 211,22 KB

Nội dung

Most of the math symbols you are familiar with also work with numeric variables. The plus sign ( +) is used for addition, the minus sign (-) indicates subtraction, the asterisk ( *) multiples, and the forward slash (/) divides. The remainder of the pro- gram illustrates how PHP does subtraction, multiplication, and division. Creating a Form to Ask a Question It’s very typical for PHP programs to be made of two or more separate documents. An ordinary HTML page contains a form, which the user fills out. When the user presses the submit button, the information in all the form elements is sent to a program specified by a special attribute of the form. This program processes the information from the form and returns a result, which looks to the user like an ordinary Web page. To illustrate, look at the whatsName.html page illustrated in Figure 2.7. The whatsName.html page does not contain any PHP at all. It’s simply an HTML page with a form on it. When the user clicks the Submit Query button, the page sends the value in the text area to a PHP program called hiUser.php. Figure 2.8 shows what happens when the hiUser.php program runs. It’s important to recognize that two different pages are involved in the transac- tion. In this section you learn how to link an HTML page to a particular script and how to write a script that expects certain form information. 33 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t FIGURE 2.7 This ordinary HTML page contains a form. Building an HTML Page with a Form Forms are very useful when you want to get information from the user. To illus- trate how this is done, look at the whatsName.html code: <html> <head> <title>What’s your name?</title> </head> <body> <h1>What’s your name?</h1> <h3>Writing a form for user input</h3> <form method = “post” action = “hiUser.php”> Please type your name: <input type = “text” name = “userName” value = “”> <br> <input type = “submit”> </form> </body> </html> 34 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 2.8 The resulting page uses the value from the original HTML form. There is only one element of this page that may not be familiar to you. Take a careful look at the form tag. It contains two new attributes. action is one of those attributes. The other, method, indicates how the data is sent to the browser. get and post are the two primary methods. post is the most powerful and flexible, so it is the one I use most often in this book. However, you see some interesting ways to use get later in this chapter in “Sending Data without a Form.” Setting the Action Attribute to a Script File The other attribute of the form tag is the action attribute. It determines the URL of a program, designed to read the page and respond with another page. The URL can be an absolute reference (which begins with http:// and contains the entire domain name of the response program), or a relative reference (meaning the pro- gram is in the same directory as the original Web page). The whatsName.html page contains a form with its action attribute set to hiUser.php. Whenever the user clicks the submit button, the values of all the fields (only one in this case) are packed up and sent to a program called hiUser.php, which is expected to be in the same directory as the original whatsName.html page. Writing a Script to Retrieve the Data The code for hiUser.php is specially built. The form that called the hiUser.php code is expected to have an element called userName. Take a look at the code for hiUser.php and see what I mean. 35 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t IN THE REAL WORLD Some PHP servers have turned off the ability to automatically create a variable from a form. You might be able to convince your server administrator to turn on register_globals in the PHP.INI file. If not, here’s a workaround: If your form has a field called userName, add this code to the beginning of the program that needs the value of that field: $userName = $_REQUEST[“userName”]; Repeat this code for every variable you wish to pull from the original form. For a complete explanation of this code, skip to chapter 5, “Better Arrays and String Handling.” In that chapter you also find a routine for automatically extracting all a form’s fields, even if you don’t know the field names. 36 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r <html> <head> <title>Hi User</title> </head> <body> <h1>Hi User</h1> <h3>PHP program that receives a value from “whatsName”</h3> <? print “<h3>Hi there, $userName!</h3>”; ?> </body> </html> Like many PHP pages, hiUser.php is mainly HTML. The only thing that’s different is the one print statement, which statement incorporates the variable $userName. The puzzling thing: no other mention of the variable anywhere in the code. When a user submits a form to a PHP program, the PHP processor automatically creates a variable with the same name as every form element on the original HTML page. Since the whatsName.html page has a form element called userName, any PHP program that whatsName.html activates automatically has access to a vari- able called $userName. The value of that variable is whatever the user has entered into the field before pressing the submit button. Sending Data without a Form It can be very handy to send data to a server-side program without using a form. This little-known trick can really enhance your Web pages without requiring a lick of PHP programming. The Link Demo page (linkDemo.html) shown in Figures 2.9 and 2.10 illustrate this phenomenon. Understanding the get Method All the links in the linkDemo.html page use a similar trick. As you recall from earlier in the chapter, form data can be sent to a program through two different methods. The post method is the technique usually in your forms, but you’ve been using the get method all along, because normal HTML requests actually are get requests. The interesting thing is that you can send form data to any program that knows how to read get requests by embedding the request in your URL. As an experiment, switch the method attribute of whatsName.html so the form looks like this: <form method = “get” action = “hiUser.php”> 37 C h a p t e r 2 U s i n g V a r i a b l e s a n d I n p u t FIGURE 2.9 The links on this page appear ordinary, but they are unusually powerful. FIGURE 2.10 When I clicked the Hi Elizabeth link, I was taken to the hiUser program with the value Elizabeth automatically sent to the program! . contain any PHP at all. It’s simply an HTML page with a form on it. When the user clicks the Submit Query button, the page sends the value in the text area to a PHP program called hiUser .php. Figure. Data The code for hiUser .php is specially built. The form that called the hiUser .php code is expected to have an element called userName. Take a look at the code for hiUser .php and see what I mean. 35 C h a p t e r. User</h1> <h3> ;PHP program that receives a value from “whatsName”</h3> <? print “<h3>Hi there, $userName!</h3>”; ?> </body> </html> Like many PHP pages, hiUser .php is

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

TỪ KHÓA LIÊN QUAN