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

PHP 5/MySQL Programming- P13 pdf

5 270 1

Đ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 214,27 KB

Nội dung

Run the page again. It works the same as before, but the URL of the resulting page looks like this (presuming you said the user’s name is Andy): http://127.0.0.1/phab/ph02/hiUser.php?userName=Andy The get method stashes all the form information into the URL using a special code. If you go back to the whatsName page and put in Andy Harris, you get a slightly different result: http://127.0.0.1/phab/ph02/hiUser.php?userName=Andy+Harris The space between Andy and Harris was converted to a plus sign because space characters cause a lot of confusion. When form data is transmitted, it often undergoes a number of similar transformations. All the translation is automatic in PHP programming, so you don’t have to worry about it. Using a URL to Embed Form Data If you understand how embedded data in a URL works, you can use a similar tech- nique to harness any server-side program on the Internet (presuming it’s set up to take get method data). When I examined the URLs of Google searches, I could see my search data in a field named q (for query, I suppose). I took a gamble that all the other fields would have default values, and wrote a hyperlink that incor- porates a query. My link looked like this: <li><a href = “http://www.google.com/search?q=php”> Google search for “php”</a></li> Whenever the user clicks this link, it sets up a get method query to Google’s search program. The result is a nifty Google search. One fun thing you might want to do is figure out how to set up canned versions of your most common queries in various search engines, so you can get updated results with one click. Figure 2.11 illustrates what happens when the user clicks the Google search for “php” link in the linkDemo page. Figure 2.12 shows the results of this slightly more complex search. <li><a href = “http://www.google.com/search?q=programming for the absolute beginner”> Google search for “programming absolute beginner”</a></li> This approach has a down side. The program owner can change the program with- out telling you, and your link will no longer work correctly. Most Web programmers assume that their programs are called only by the forms they originally built. TRAP 38 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 The other thing to consider is that people can do this with your programs. Just because you intend for your program to be called only by a form, doesn’t mean that’s how it always works. Such is the vibrant nature of the free-form Internet. 39 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.11 The link runs a search on www.google.com for the term php. FIGURE 2.12 The Google search for programming absolute beginner shows some really intriguing book offerings! 40 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 Working with Multiple Field Queries As one more practical example, the code for the National Weather Service link looks like this: <li><a href = “http://www.crh.noaa.gov/data/forecasts/INZ039.php?warncounty=INC057&city= Noblesville”> National Weather Service Forecast</a> for Noblesville, Indiana. While this link looks a little more complex, it doesn’t require any special knowl- edge. I simply searched the National Weather Service Web site until I found the automatically generated page for my hometown. When I looked at the URL that resulted, I was pleased (but not surprised) to see that the page was generated by a PHP script. (Note the .php extension in the URL.) I copied the link from my browser and incorporated it into linkDemo.html. The weather page is automati- cally created by a PHP program based on two inputs (the county and city names). Any time I want to see the local weather, I can recall the same query even though the request doesn’t come directly from the National Weather Service. This is a really easy way to customize your Web page. I’ve never actually seen the program, but I know the PHP program requires two fields because I looked carefully at the URL. The part that says warncounty=INCO57 indicates the state and county (at least that’s a reasonable guess), and the city=Noblesville indicates the city within the county. When a form has two or more input elements, the ampersand ( &) attaches them, as you can see in the National Weather Service example. Reading Input from Other Form Elements A PHP program can read the input from any type of HTML form element. In all cases, the name attribute of the HTML form object becomes a variable name in PHP. In general, the PHP variable value comes from the value property of the form object. Introducing the borderMaker Program To examine most of the various form elements, I built a simple page to demon- strate various attributes of cascading style sheet (CSS) borders. The HTML program is shown in Figure 2.13. Building the borderMaker.html Page The borderMaker.html page contains a very typical form with most of the major input elements in it. The code for this form is as such: <html> <head> <title>Font Choices</title> </head> <body> <center> <h1>Font Choices</h1> <h3>Demonstrates how to read HTML form elements</h3> <form method = “post” action = “borderMaker.php”> <h3>Text to modify</h3> <textarea name = “basicText” rows = “10” cols = “40”> Four score and seven years ago our fathers brought forth on this 41 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.13 The borderMaker HTML page uses a text area, two list boxes, and a select group. continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure. </textarea> <table border = 2> <tr> <td><h3>Border style</h3></td> <td colspan = 2><h3>Border Size</h3></td> </tr> <tr> <td> <select name = borderStyle> <option value = “ridge”>ridge</option> <option value = “groove”>groove</option> <option value = “double”>double</option> <option value = “inset”>inset</option> <option value = “outset”>outset</option> </select> </td> <td> <select size = 5 name = borderSize> <option value = “1”>1</option> <option value = “2”>2</option> <option value = “3”>3</option> <option value = “5”>5</option> <option value = “10”>10</option> </select> </td> <td> <input type = “radio” name = “sizeType” value = “px”>pixels<br> <input type = “radio” name = “sizeType” 42 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 . generated by a PHP script. (Note the .php extension in the URL.) I copied the link from my browser and incorporated it into linkDemo.html. The weather page is automati- cally created by a PHP program. Elements A PHP program can read the input from any type of HTML form element. In all cases, the name attribute of the HTML form object becomes a variable name in PHP. In general, the PHP variable. My link looked like this: <li><a href = “http://www.google.com/search?q =php > Google search for php </a></li> Whenever the user clicks this link, it sets up a get method

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

TỪ KHÓA LIÊN QUAN