Low cost ecommerce web development India flash website design

Note

Don't be alarmed at the slashes that appear in some of these tags (e.g.

<br />). The new XHTML standard for coding Web pages calls for these

in any tag that does not have a closing tag, which includes <input> and

<br> tags, among others. Current browsers do not require you to use the

slashes, of course, but for the sake of standards-compliance, the HTML

code in this book will observe this recommendation. Feel free to leave

the slashes out if you prefer — I agree that they're not especially nice to

look at.

This form has the exact same effect as the second link we looked at (with

firstname=Kevin&lastname=Yank in the query string), except that you can enter

whatever names you like. When you click the submit button (which has a label

of "GO"), the browser will load welcome3.php and automatically add the variables

and their values to the query string for you. It retrieves the names of the variables

57

User Interaction and Forms

from the name attributes of the input type="text" tags, and it obtains the values

from the information the user typed into the text fields.

The method attribute of the form tag is used to tell the browser how to send the

variables and their values along with the request. A value of get (as used above)

causes them to be passed in the query string (and appear in PHP's $_GET array),

but there is an alternative. It's not always desirable—or even technically feasible

—to have the values appear in the query string. What if we included a <textarea>

tag in the form, to let the user enter a large amount of text? A URL that

contained several paragraphs of text in the query string would be ridiculously

long, and would exceed by far the maximum length of the URL in today's browsers.

The alternative is for the browser to pass the information invisibly, behind the

scenes. The code for this looks exactly the same, but where we set the form

method to get in the last example, here we set it to post (welcome4.html):

<form action="welcome4.php" method="post">

First Name: <input type="text" name="firstname" /><br />

Last Name: <input type="text" name="lastname" /><br />

<input type="submit" value="GO" />

</form>

As we're no longer sending the variables as part of the query string, they no longer

appear in PHP's $_GET array. Instead, they are placed in another array reserved

especially for 'posted' form variables: $_POST2. We must therefore modify welcome3.

php to retrieve the values from this new array (welcome4.php):

<?php

$firstname = $_POST['firstname'];

$lastname = $_POST['lastname'];

echo( "Welcome to my Website, $firstname $lastname!" );

?>

This form is functionally identical to the previous one. The only difference is

that the URL of the page that's loaded when the user clicks the "GO" button will

not have a query string. On the one hand, this lets you include large values, or

sensitive values (like passwords) in the data that's submitted by the form, without

their appearing in the query string. On the other hand, if the user bookmarks

the page that results from the form's submission, that bookmark will be useless,

as it doesn't contain the submitted values. This, incidentally, is the main reason

that search engines like Googleii use the query string to submit search terms. If

2Prior to PHP 4.1, 'posted' form variables were available in the $HTTP_POST_VARS array. This array

remains available in current versions of PHP for backwards compatibility.

iihttp://www.google.com/

58

Getting Started with PHP

you bookmark a search results page on AltaVista, you can use that bookmark to

perform the same search again later, because the search terms are contained in

the URL.

Sometimes, you want access to a variable without having to worry about whether

it was sent as part of the query string or a form post. In cases like these, the

special $_REQUEST3 array comes in handy. It contains all the variables that appear

in both $_GET and $_POST. With this variable, we can modify welcome4.php one

more time so that it can receive the first and last names of the user from either

source (welcome5.php):

<?php

$firstname = $_REQUEST['firstname'];

$lastname = $_REQUEST['lastname'];

echo( "Welcome to my Website, $firstname $lastname!" );

?>

That covers the basics of using forms to produce rudimentary user interaction

with PHP. I'll cover more advanced issues and techniques in later examples.

website designer freelance ASP PHP ecommerce web developer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110