715Chapter 38PHP and JavaScriptfor (var x = 0; x < boxVals.length; x++) { var valArray = boxVals[x].name+ [] ; boxVals[x].name = valArray; } } // -->
Posted in Domain | No Comments »
714Part IVConnectionsPassing data back to PHP from JavaScriptFinally, we close the data loop by passing form values back to PHP with JavaScript. Listings38-5, 38-6, and 38-7 use JavaScript to force at least one check box to be checked at all times. Inaddition, it passes an array to a PHP script. We ve chosen to use frames here to maximize thespeed of the changes, and we wrote all values out by hand for clarity, rather than assemblingthem dynamically from a data source. Listing 38-5: Frameset (sandwich_frames.html)
711Chapter 38PHP and JavaScript} function jmp(form, elt) { // The first parameter is a reference to the form. if (form != null) { with (form.elements[elt]) { if (0 <= selectedIndex) location = options[selectedIndex].value; } } } var catsIndex = -1; var itemsIndex; if (v) { // ns 2 fixfunction newCat(){ catsIndex++; a[catsIndex] = new Array(); itemsIndex = 0; } // Andrew chose to name this function O , presumably standing// for Option . It s not a zero, here or in the array below! function O(txt,url) { a[catsIndex][itemsIndex]=new myOptions(txt,url); itemsIndex++; } function myOptions(text,value){ this.text = text; this.value = value; } // fill arrayIf you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.
710Part IVConnectionsListing 38-4(continued) //–>
You will doubtless have realized by now that a similar technique would be valuable even if youwere making a straight JavaScript form (such as by using the onSubmitevent handler ratherthan onChange). It would enable you to make a basic JavaScript function more flexible byallowing PHP to change variable values within the function before it was sent to the browser. So feel free to use PHP to output straight JavaScript using variables from a data source, if you like. Dynamically generated formsYou can usefully extend this train of programming thought even further by setting up a seriesof dynamic drop-downs that change according to previous form inputs. PHP will fetch all thedata from a database and load it into the HTML page, whereas JavaScript will decide whichdata set should be visible under various conditions. If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.
707Chapter 38PHP and JavaScriptYou could use a similar division of labor with form validation. If JavaScript is enabled, youcan use it to make sure zip codes have nine digits, phone numbers have ten digits, and e-mailaddresses have both an @and a .. If JavaScript is not enabled, you can write a little PHPscript that will do the same things when the form is submitted and return the form with warn- ings if the values are bad. JavaScript form validation should be relied on only for quick convenience reminders, neverfor data sanitization. See Chapter 29 for more information on data security. Another kind of form is basically arithmetic a shopping cart with running totals or a mort- gage payment calculator. Again, you can combine both JavaScript and PHP in an arithmeticform to cover all the bases. Finally, there is one use where PHP is so much faster that you might want to replace JavaScriptaltogether: browser sniffing. This is done to send different versions of a file (for instance, astylesheet) to a visitor depending on which browser she s using. Server-side browser sniffingis vastly more efficient than client-side because no text is sent until the sniff has occurred. AJavaScript browser sniff can amount to hundreds of lines of JavaScript, which must be sent onevery download whether the correctly browser version has been detected or not. Listing 38-2shows a very simple server-side browser sniff. Listing 38-2:A server-side browser sniff (browsersniff.php) 0) { header( Location: index_ie.html ); } elseif (strpos($HTTP_USER_AGENT, Gecko ) > 0) { header( Location: index_moz.html ); } ?> Static Versus Dynamic JavaScriptAlthough the static JavaScript-PHP form in Listing 38-1 is handy for many applications, there sone big problem with it: You have to maintain it by hand. Every time you decide to add a newpage to your site, you ll have to remember to manually add it to the drop-down list. Big deal, you re thinking but these are the little things that become time-sucking nightmares whenyou re running a huge and high-traffic site. With PHP and a database, you can update some of your JavaScript automatically when newdata is stored in the database or, as we might say, dynamically. You want to take this optionwhenever possible, as it will help you save time in the long run. Listing 38-3 is how you drewrite the form in Listing 38-1 for even better client/server integration. Caution43 You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
706Part IVConnectionstake advantage of the zippier method, generally without even noticing that they ve had achoice. If not, at least you won t suffer the ignominy of totally locking them out of your site sfunctionality. A perfect example is the double-barreled pull-down menu for site navigation. JavaScript givesyou an instant redirect, whereas PHP provides the same result after a longer wait for thosewithout JavaScript-enabled browsers. This trick takes advantage of the fact that JavaScripthas event handlers (for example, onChange) that work off the structure of HTML forms with- out requiring an actual button-clicking submission. Therefore, the Submit button can bereserved for PHP s use. Listing 38-1 shows an HTML page that uses a JavaScript onChangeredirect and, if that doesn t work, a PHP form handler. Listing 38-1:A JavaScript and PHP navigation form (navigation.html)
The PHP form handler file, called redirect.php, need only have two lines: Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.
You are currently browsing the J2Ee Hosting, Sun Java Developing, Domain Name Hosting Blog weblog archives for November, 2007.