<HTML>
<FORM>
Please type your name here:
<INPUT TYPE=TEXT NAME=username>
<INPUT TYPE=SUBMIT VALUE="Submit data">
</FORM>
<P>
<?php
print("You typed $username.\n");
?>
</HTML>
When you load this page the first time, you will see an empty input box, a button containing the label Submit data, and the text You typed . Then if you fill in Joe Smith and click the button, the page will be redisplayed with the text You typed Joe Smith.
When the page containing the form is submitted, each PHP variable with the same name as one of the NAME= fields in the form is initialized with the value placed in the form by the client.
Therefore, the printing after the form is always of what the user entered before the last time s/he clicked the submit button. You must remember this when designing your PHP code, to avoid making the user confused.
You can make the result of submitting a form be a completely different
web page by writing <FORM ACTION="process.php"> for example.
http://ieng9.ucsd.edu/~cs134s/test.php?username=Joe+Smith
The browser packages the information typed into the form as name/value pairs in the URL. To avoid unprintable characters, there is a convention where blanks are changed to + symbols and other characters are also encoded in special ways. On the server, PHP reverses this encoding automatically before initializing variables with the right names.
Browsers treat URLs that include GET information in the same way as URLs that designate static web pages. So, the browser may cache the value of these URLs. This can be incorrect if the server wants to return different information in response to the same form input, for example because time has passed.
If you write <FORM METHOD="POST"> then the browser sends
the field values to the server not as part of the URL, but as separate
messages in the HTTP protocol. In this case the field values sent
are not visible to the human user, and the web page returned by the server
is never cached.
See http://www.php.net/manual/en/language.variables.predefined.php
Instead of $username, you need to write $_GET['username']
Alternatively, you can use the PHP function named import_request_variables
The db client is a PHP script. The script sends commands written
in the SQL language. The db server sends responses. A response
is often a record containing data.
if (!($link=mysql_connect($hostname, $username, $password)))
printerror("cannot connect
to $hostname by $username");
if (!mysql_select_db($dbname, $link))
printerror("cannot select
$dbname database");
Do not use pconnect. It is supposed to be more efficient but it
has some bugs that are only visible when many scripts are making thousands
of connections to the database server.
All database operations are specified using a language of commands called SQL. For example:
drop table if exists temp;A query allows data to be retrieved based on its content, not its storage location. For example:
select author, date from messages
where date > '2000-11-11'