There are two stages to receiving data from Web users: the first is to provide an interface to interact with, and the second is to retrieve this information. The most common way to do this is through the Common Gateway Interface (CGI), supported by PHP.
User input and HTML
To provide an interactive interface, Web application designers and PHP developers generally use HTML forms. The HTML form for a search engine might look something like this
<FORM METHOD=GET ACTION="/search.php">
Search for: <INPUT TYPE=TEXT NAME=query SIZE=20>
<INPUT TYPE=SUBMIT NAME=submit VALUE=Search>
</FORM>
The first line of HTML sets up the form, declaring the client/server interface method (basically, how the form data is sent to the search script through CGI) and the public location of the script itself. The second line gives the user a text box (defined by TYPE=TEXT), 20 characters in length, where the query text should be input. The name of this text box is 'query'.
The third line creates a different input type, this time SUBMIT - that is, a submit button. The final line closes the form.
When the user fills in the form by entering a query in the query text box, and presses the SUBMIT button, the Web browser sends the information to the search script, search.php. Since the METHOD is GET, this is done by encoding the value of the text box 'query' in the URL. If a user submits a query like 'PHP', the resulting URL may be something like this: http://www.mysearchengine.com.au/search.php?query=PHP&submit=Search.
Notice that the data submitted by the user is sent in 'name=value' pairs, separated by an ampersand (&). In the first pair, 'name' is query and 'value' is the user data.
USER INPUT AND PHP With the help of the Web server, PHP initialises variables names based on the 'name=value' pairs from the HTML form. In the example above, the PHP script search.php would be able to access the query data submitted by the user by accessing the variable $query. The following example illustrates this.
<HTML>
<BODY>
<?
if(!isset($submit)) {
/* User has not submitted form data */
?>
<FORM METHOD=POST ACTION="<? echo $PHP_SELF; ?>">
Name: <INPUT TYPE=TEXT NAME=name SIZE=20 /><BR />
Phone: Area Code: <INPUT TYPE=TEXT NAME=ac SIZE=2>
Number: <INPUT TYPE=TEXT NAME=ph SIZE=9 /><BR />
Mobile: <INPUT TYPE=TEXT NAME=m SIZE=11 /><BR />
Email: <INPUT TYPE=TEXT NAME=e SIZE=25 /><BR />
<INPUT TYPE=SUBMIT NAME=submit VALUE="Submit"/>
</FORM>
<?
} else {
$fp = fopen("contact.data","a");
$s = "--\nN$name\nA$ac\nP$ph\nM$m\nE$e\n";
fputs($fp,$s);
fclose($fp);
echo "User data:\nname = $name\nac = $ac\nph = $ph\nm = $m\ne = $e\nsubmit = $submit\n";
}
?>
</BODY>
</HTML>
To see how this works, save this script as contact.php and upload it to your PHP-enabled Web server.
Request contact.php from your Web server. You will see a standard HTML form, which is sent to your Web browser because the condition !isset($submit) is true. The function isset() tests whether the variable given as the argument was previously initialised. If you submit the form, the variable submit is set to 'Submit' by the last INPUT tag, as described above.
There are two other points of interest in the HTML section of the code. First, the ACTION attribute is filled out with the result of the $PHP_SELF variable. This variable is the path to and the name of the script being executed. If the URL you are requesting is www.mywebsite.com/php/contact.php, then the value of $PHP_SELF is '/php/contact.php'.
The other point is that slashes occur at the end of some HTML tags. The latest HTML recommendation, XHTML 1.0, requires that an element <E> with no corresponding </E> tag must be written as <E /> (more at www.w3.org/XHTML/).
When the user submits the form, the variable $submit is set, so control is given to the code at 'else'. This code opens a file, contacts.data, and writes a string to it. Note that contacts.data is opened in the 'a' mode, that is, append. The variables are written to the file, one per line, with a character prefixing them. This leading character is different for each variable, and gives some structure to each entry, making it easier to parse. By testing the first character of a line, a parser can tell which variable it is dealing with: N for name, A for area code, P for phone, etc.
With such predictable data, a structure this robust is not really necessary. A parser could be designed to assume that the first line after '--' is the name, the second line the area code and so on. However, it is good practice always to design applications with good data storage formats.
Discover how SOA can create smarter outcomes for your business.
Attend and learn:
- How SOA is helping leading companies to become more agile
- Where you should be applying SOA processes in your company
- The top SOA implementation mistakes to avoid
Click here for more information.
- +
Computerworld Live Podcast #97: The Future of Enterprise Networking 25/07/2008 09:45:36
This week CW Live chats with Mark Thompson, global sales and marketing manager for HP ProCurve, on the future of the enterprise networking. Mark discusses the trends we can expect to see in the near future and how the right infrastructure can ensure your enterprise network is secure. - +
Computerworld Live Podcast #96: Security at the Edge 11/06/2008 09:22:22
CW Live speaks with Amol Mitra, HP ProCurve Director of Marketing for Asia Pacific and Japan. Today's topic: how enterprises are starting to shift away from simply controlling security via server logins, firewalls and moving to more adaptive security frameworks. - +
Data Management Edition #10: Multi-Petascale Systems 02/05/2008 09:12:33
This week we look at sustainability and the development of multicore technologies to build multi-petascale systems. - +
IT Security Edition #11: How to poison the Storm botnet 01/05/2008 08:51:55
This week CW Live presents a case study on how to poison the notorious Storm botnet . Plus we take a look at Cisco's plans for Ironport. - +
IT Security Edition #10: Cyber-battles fought and won 24/04/2008 11:09:47
Vendors bow to end user pressure to improve product security, and we take a look at the latest concepts shaping the cyber-battlefield of the future.
International researchers gather in Sydney to preview the clever web 2008-12-05 09:48:00+11
Borderless corporate networks to shift focus to secure content management in Australia in 2009 2008-12-04 16:06:00+11
IDC Says Asia/Pacific Excluding Japan IT Market Will Remain The Bright Spot... 2008-12-04 15:04:00+11
MySpot SOS "Panic Button" Smartphone Application could save lone worker lives 2008-12-04 13:34:00+11
Charles Sturt University Commences Unified Communications Deployment With Interactive Intelligence 2008-12-04 08:30:00+11
Solve Exchange Mailbox Storage Issues Once and for All
Join industry expert Bob Spurzem and Chuck Arconi of Fox Hollow to discover how to reduce Exchange total storage and keep it at a manageable level. Learn how Exchange storage growth can be contained without sacrificing security and accessibility.













Comments
helpful strategies to experts
helpful strategies to experts but not to the beginners!
------------------------
sea plants...sea grapes...plant roots...phytoplanktons...sea plant pictures...seagrass...seaweed...easy aquarium plants...deep sea plants...Limu Moui...Landscaping...End of sea plants...underwater plants...Sea coral...Sea crabs
helpful strategies to experts
helpful strategies to experts but not to the beginners!
------------------------
sea plants...sea grapes...plant roots...phytoplanktons...sea plant pictures...seagrass...seaweed...easy aquarium plants...deep sea plants...Limu Moui...Landscaping...End of sea plants...underwater plants...Sea coral...Sea crabs