Archive for August, 2007

130Part IPHP: The BasicsListing 7-2(continued) $footer_str = (Web hosts)

Friday, August 31st, 2007

130Part IPHP: The BasicsListing 7-2(continued) $footer_str = <<< EOFOOTER

EOFOOTER; // Screen 1: quiz form// ——————- $quiz_str = <<< EOQUIZ

How geeky are you?

0. Have you ever had a dream in which you were debugging?
Yes

1. Do you know the name of the company founded by DannyHillis?
Yes

2. Can you edit a file in both emacs and vi without recourse toany documentation?
Yes

3. Is the computer you re using at this moment hooked up to aKVM switch?
Yes

4. Are you wearing a logowear T-shirt?
Yes

5. Have you ever written a chess program?
Yes

6. Have you ever set up an SMTP server?
Yes

7. Have you ever discussed the merits of a commercial LISPimplementation?
Yes

8. Have you ever used the phrase I can do that in two lines of09
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

129Chapter 7Passing Information between (Web site designers) Pagesbecause some browsers don t

Thursday, August 30th, 2007

129Chapter 7Passing Information between Pagesbecause some browsers don t actually submit the Submitvalue if a user hits Enter instead ofclicking the button. Using array variables with formsIn all the examples so far, each form field created a variable of the string or integer types. This implies that there is a one-to-one relationship between a form field and a form-handlervariable. But PHP also allows you to post an array-type variable. (If you don t yet have a good grip on arrays, come back to this section after you read Chapter 9). Listing 7-2 is an example of a script that creates an array from the names of the form fields inan HTML form. Listing 7-2:Form passing an array of variables (geek_quiz.php)


EOHEADER; // The footer which appears in both cases// ————————————– Continued09
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

128Part IPHP: The BasicsFigure (Web site domain) 7-1:A form using the

Wednesday, August 29th, 2007

128Part IPHP: The BasicsFigure 7-1:A form using the POST method with VALUE attributesConsolidating forms and form handlersAs you can see in the preceding example, it is often handy to make the HTML form and theform handler into one script. This practice has many advantages, such as making it easier tochange the name of the file without harming functionality, making it easier to display errormessages and prefilled form fields, and achieving better control over your variable names- pace. Suppose you are making a login form that redisplays with an error message if the loginis unsuccessful. If you have separate forms and form handlers, you ll probably have to dosomething yucky with GETvars and redirection. If you consolidate, it s very simple to controlthe display without these machinations. (See Chapter 44 for an example of this very usage ina login form.) To see how these techniques can be used with data from MySQL, see Chapter 17. When you consolidate, generally the form-handling code should come before the form dis- play. This order may be something of a shift in thinking for those who are used to writing theform before the handler, but if you think it through, you will see the logic of the practice. Youhave to give yourself an opportunity to set variables and make choices before you can decidewhat to show the user. This is especially relevant if you will be redirecting the user to a differ- ent page under certain circumstances, via the header()function, because this decision pointmust come before any HTML output has been displayed to the browser. Generally there are two ways you can check to see whether you re displaying a form for thefirst time or whether it s already been submitted at least once. Either you can use the Submitbutton, as we do in the preceding example, or you can set a hidden variable if you tend tohave all your Submit buttons say the same thing (like Submit ). The latter method is safer, Cross- Reference09
In case you need affordable webhost to host your website, our recommendation is ecommerce web host services.

Web host music - 127Chapter 7Passing Information between Pageswhile ($YearCount A retirement-savings

Wednesday, August 29th, 2007

127Chapter 7Passing Information between Pageswhile ($YearCount <= $Years) { $Total = round($Total * (1.0 + $AnnGain/100) + $_POST[ Contrib ]); $YearCount = $YearCount + 1; } } ?>

A retirement-savings calculator

Fill in all the values (except Nest Egg ) and see how much money you ll have for your retirementunder different scenarios. You can change the values andresubmit the form as many times as you like. You must fillin the two Age variables. The Annual return variable hasa default inflation-adjusted value (7% = 8% growth minus 1% inflation) which you can change to reflect your greateroptimism or pessimism.

METHOD= POST >

Your age now: >

The age at which you plan to retire: >

Annual contribution: >

Annual return: > %

NEST EGG:

Figure 7-1 shows the result of the Listing 7-1.09
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

126Part IPHP: The BasicsAnother thing to keep in (Web hosting bandwidth)

Tuesday, August 28th, 2007

126Part IPHP: The BasicsAnother thing to keep in mind when creating your HTML forms is that, if you ever want thisform to be displayed with prefilled inputs, you need to set the VALUEattribute. This is particu- larly relevant to two kinds of forms: those that are used to edit data from a database; andthose that are intended to possibly be submitted more than once. The latter case is very com- mon in situations where a form should redisplay on error with values already prefilled forinstance, a registration form that will not work until the user provides a valid e-mail address or other required data. For example, the form in Listing 7-1 (which represents a retirement savings calculator) isdesigned to be submitted multiple times while the user fiddles around with the values. Everytime you submit the form, the values from the previous go-round will be filled in for you auto- matically. Note the use of the VALUEattribute in the form fields in this code sample. Listing 7-1:Form with prefilled values (retirement_calc.php) A POST example: retirement savings worksheet Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Professional web hosting - 125Chapter 7Passing Information between PagesWe use the POSTmethod

Monday, August 27th, 2007

125Chapter 7Passing Information between PagesWe use the POSTmethod consistently in this book for form handling especially whenputting data into a system via a file write or SQL INSERT. We use GETonly for site navigationand Search boxes in other words, for pulling data back out of a data store and displaying it. All the rest of the forms in this chapter use the POSTmethod. Formatting Form VariablesPHP is so efficient at passing data around because the developers made a very handy but (intheory) slightly sketchy design decision. PHP automatically, but invisibly, assigns the vari- ables for you on the new page when you submit a data set using GETor POST. Most of PHP scompetitors make you explicitly do this assignment yourself on each page; if you forget to doso or make a mistake, the information will not be available to the processing agent. PHP isfaster, simpler, and mostly more goof-proof. But because of this automatic variable assignment, you need to always use a good NAMEattribute for each INPUT. NAMEattributes are not strictly necessary in HTML proper yourform will render fine without them but the data will be of little use because the HTML form-field NAMEattribute will be the variable name in the form handler. In other words, in this form:

METHOD= POST >
the text field named emailwill cause the creation of a PHP variable called $_POST[ email ] (or $HTTP_POST_VARS[ email ]if you use the older style of variable arrays; or just $emailif you have register_globals turned on) when the form is submitted. Similarly, the submitbutton will lead to the creation of a variable called $_POST[ submit ]on the next page. Thename you use in the HTML form will be the name of your variable in the PHP form handler. $HTTP_POST_VARS, $HTTP_SERVER_VARS, and the whole family of these long-form pre- defined variables are deprecated in PHP5. If you are already an experienced PHP program- mer, perhaps with a large body of previously written code lying around, you might want tothink about rewriting now for backward compatibility. They are supported for the time being, but their days are numbered. Use $_POST, $_GET, and friends instead. Remember that you cannot use a variable name beginning with a number so you should notname your form field something like 5(you laugh, but we ve seen people try to do it) andPHP variable names are case sensitive. Also, pleasetry to use informative variable namesrather than a succession of form fields named myvarand e. It s a good idea to standardize how you name form variables, to make your code more read- able and so that you spend less time flipping back to the form itself when you are supposedto be writing code to process that form. For example, you might precede all form variableswith frmto indicate their source. You might then consistently use the first few letters of eachidentifying word for what a field does, for example, frmNameFirst, frmOfficeAdd, frmHomeAdd, and so on. The specific standard you set is less important than having a stan- dard to begin with. TipCaution09
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

124Part IPHP: The BasicsBut even with this solution, (Crystaltech web hosting)

Sunday, August 26th, 2007

124Part IPHP: The BasicsBut even with this solution, you still have to tend part of your garden by hand: making sureeach includefile is properly formatted in HTML, adding a new link to the navbar each time youadd a new page to the site, and other such chores. Following the general rule to separate formand content as much as is feasible, you might choose to go to another level of abstraction witha database. In that case, a URL such as http://localhost/topic.php?topicID=2wouldpoint to a PHP template that makes database calls. (Using a number variable rather than a word makes for faster database interaction.) This system could also automatically add a link tothe navbar whenever you added new topics to the database, so it could produce Web pagesentirely without ongoing human intervention (all right, maybe entirelyis an exaggeration butwith significantly fewer person-hours of grunt labor). POST ArgumentsPOSTis the preferred method of form submission today, particularly in nonidempotentusages(those that will result in permanent changes), such as adding information to a database. Theform data set is included in the body of the form when it is forwarded to the processing agent(in this case, PHP). No visible change to the URL will result according to the different datasubmitted. The POSTmethod has these advantages: .It is more secure than GETbecause user-entered information is never visible in the URLquery string, in the server logs, or (if precautions, such as always using the passwordHTML input type for passwords, are taken) onscreen. .There is a much larger limit on the amount of data that can be passed (a couple of kilo- bytes rather than a couple of hundred characters). POSThas these disadvantages: .The results at a given moment cannot be bookmarked. .The results should be expired by the browser, so that an error will result if the useremploys the Back button to revisit the page. .This method can be incompatible with certain firewall setups, which strip the formdata as a security measure. Get and Post bothDid you know that with PHP you can use both GETand POSTvariables on the same page? Youmight want to do this for a dynamically generated form, for example. But what if you (deliberately or otherwise) use the same variable name in both the GETand thePOSTvariable sets? PHP keeps all ENVIRONMENT, GET, POST, COOKIE, and SERVERvariables in the$GLOBALSarray if you have set the register_globalsconfiguration directive to on in yourphp.inifile. If there is a conflict, it is resolved by overwriting the variable values in the order youset, using the variables_orderoption in php.ini. Later trumps earlier, so if you use thedefault EGPCS value, cookies will triumph over POSTs that will themselves obliterate GETs. You can control the order of overwriting by simply changing the order of the letters on the appro- priate line of this file, or even better, turning register_globalsoff and using the new PHPsuperglobal arrays instead. See the section on superglobals later in this chapter.
Go visit our java server pages services for a reliable, lowcost webhost to satisfy all your needs.

Web site translator - 123Chapter 7Passing Information between PagesOr you might decide

Sunday, August 26th, 2007

123Chapter 7Passing Information between PagesOr you might decide you needed a larger, more specialized choice of template files: vehicle_structure.phptubular_frames.incmechanical_systems.phpfriction_braking.incelectrical_systems.phpsolar_array.incracing.phprace_strategy.incA simple template file might look something like this (because we haven t included the neces- sary .inctext files, this example will not actually work): Solar-car topics

Friction braking
Steering
Suspension
Tires and wheels

Notice that the links on the navbar, when clicked, will be handled by the browser as if theywere the product of a GETsubmission.
We recommend high quality webhost to host and run your jsp application: christian web host services.

Domain and web hosting - 122Part IPHP: The BasicsThe GETmethod of form handling

Saturday, August 25th, 2007

122Part IPHP: The BasicsThe GETmethod of form handling offers one big advantage over the POSTmethod: It con- structs an actual new and differentiable URL query string. Users can now bookmark this page(and thus find the oh-so-necessary encouraging word when their team starts to fade in thedoldrums of August). The result of forms using the POSTmethod is not bookmarkable. Just because you canachieve the desired functionality with GETarguments doesn t mean youshould. The disadvantages of GETfor most types of form handling are so substantial that theoriginal HTML 4.0 draft specification deprecated its use in 1997. These flaws include: .The GETmethod is not suitable for logins because the username and password are fully visible onscreen as well as potentially stored in the client browser s memory asavisited page. .Every GETsubmission is recorded in the Web server log, data set included. .Because the GETmethod assigns data to a server environment variable, the length ofthe URL is limited. You may have seen what seem like very long URLs using GET butyou really wouldn t want to try passing a 300-word chunk of HTML-formatted proseusing this method. The original HTML spec called for query strings to be limited to 255 characters. Although thisstricture was later loosened to mere encouragement of a 255-character limit, using a longerstring is asking for trouble. The GETmethod of form handling had to be reinstated by the W3C after much outcry, largelybecause of the bookmarkability factor. Despite that it s still implemented as the defaultchoice for form handling in all browsers, GETnow comes with a strong recommendation todeploy it in idempotentusages only in other words, those that have no permanent sideeffects. Putting two and two together, the single most appropriate form-handling use of GETis the search box. Unless you have a compelling reason to use GETfor non-search-box formhandling, use POSTinstead. A Better Use for GET-Style URLsAlthough the actual GETmethod of form handling is deprecated, the style of URL associatedwith it turns out to be very useful for site navigation. This is especially true for dynamicallygenerated sites such as those often constructed with PHP, because the appended-variablestyle of URL works particularly smoothly with a template-based content-development system. As an illustration, imagine you are the proud proprietor of an information-rich Web site aboutsolar cars. You ve toiled long and hard over informative and attractive pages such as these: suspension_design.htmlwindtunnel_testing.htmlfriction_braking.htmlBut as your site grows, a flat-file site structure like this can take a lot of time to administer, aseven the most trivial changes must be repeated on every page. If the structure of these pagesis very similar, you might want to move to a template-based system with PHP. You might decide to utilize a single template with separate text files for each topic (containinginformation, photos, comments, and so on): topic.phpsuspension_design.incwindtunnel_testing.incfriction_braking.incCaution09
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

Adelphia web hosting - 121Chapter 7Passing Information between PagesThe browser thus constructs

Friday, August 24th, 2007

121Chapter 7Passing Information between PagesThe browser thus constructs the URL string: http://localhost/baseball.php?Team=Cubbies&Submit=SelectIt then forwards this URL into its own address space as a new request. The PHP script towhich the preceding form is submitted (baseball.php) will grab the GETvariables from theend of the request string, stuff them into the $_GETsuperglobal array (explained in amoment), and do something useful with them in this case, plug one of two values into atext string. Strictly speaking, the name-value pairs in a GETquery are not part of the HTTP or addressingstandards. In one of the odd footnotes of Internet history, the W3C allowed for the possibil- ity of extra data to be passed to a resource after a ?in the URI string but never specified pre- cisely what form that data should take. Usage quickly established the notion of name-valuepairs separated by ampersands, but this is not part of any W3C standard. The following code sample shows the PHP form handler for the preceding HTML form: A GET method example, part 2

Go, !

Note that the value inputted into the previous page s HTML form field named Team is nowavailable in a PHP variable called $_GET[ Team ]. Finally, you should see a page that saysGo,Cubbies!in big type. At this point, it makes some sense to explain just how to access values submitted from pageto page. This chapter discusses the two main methods for passing values: GETand POST(there are others, but they are not covered until Part III). Each method has an associatedsuperglobal array, explained in more depth in Chapter 9, which can be distinguished fromother arrays by the underscore that begins its name. Each item submitted via the GETmethod is accessed in the handler via the $_GETarray; each item submitted via the POSTmethod is accessed in the handler via the $_POSTarray. The syntax for referencing an itemin a superglobal array is simple and 100 percent consistent: $_ARRAY_NAME[ index_name ] where the index_nameis the name part of a name-value pair (for the GETmethod), or thename of an HTML form field (for the POSTmethod). As in the preceding example, $_GET[ Team ]indicates the value of the form select field called Team , sent by the GEToperation in the original file. You must use the array appropriate to the method used to send data. In this case, $_POST[ Team ]is undefined because no data was POSTed by theoriginal form. NoteTip09
If you are in need for cheap and reliable webhost to host your website, we recommend http web server services.