128Part IPHP: The BasicsFigure (Web site domain) 7-1:A form using the
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.