Professional Documents
Culture Documents
Introduction To PHP and Mysql: Working With Forms
Introduction To PHP and Mysql: Working With Forms
Example:
<?php
function initialize( $key, $value )
{
// define global variable
$GLOBALS[$key] = $value;
}
To this instead:
variables_order = "GPCSE"
Student Exercise 11.1
Summary: Write a program that displays the key/value
pairs contained in the superglobal associative array
“$_ENV”.
Requirements:
1. Bold face the variable name and the key.
2. Place a paragraph between each key/value pair.
$_ENV[key1]: value1
$_ENV[key2]: value2
...
Student Solution 11.1 (option 1)
Here is one way to do it (see file “_ENV.1.php”):
<?php
$env = $_ENV; // copy original
ksort( $env ); // sort keys
<?php
$env = $_ENV ; // copy original
ksort( $env ); // sort keys
To this instead:
short_open_tag = On
Superglobal Variable $_SERVER
Description: The PHP mechanism for extracting
information from the web server is performed via the
superglobal variable $_SERVER
Requirements:
1. Bold face the variable name and the key and place on a separate line.
2. The value should be indented and placed on a separate line.
3. Place a paragraph between each key/value pair.
$_SERVER[key1]:
value1
$_SERVER[key2]:
value2
...
Student Solution 11.2
Here is one way to do it (see file “_SERVER.php”):
<?php
$server = $_SERVER; // copy original
ksort( $server ); // sort keys
foreach ( $server as
$key => $val )
{
printf( "
<b>\$_SERVER[%s]:</b>
<dd> %s <p />\n",
$key, $val );
}
?>
HTML Search Form Example
Description: HTML forms provide a way to obtain input from
the user. That information can then be used in a calculation
or stored in a database.
<form action="search.html"
method="GET">
Search:
<input type="text"
name="search"
value="" />
<input type="submit"
value="Find!" />
</form>
Making a “Sticky” Form
Description: It is often convenient for a form
to “remember” the text from a previous
submission and set it as the default value
for the next submission. In this way, the
user does not have to re-enter the same
information again.
Search:
<input type="text"
name="find"
value="<?= $_GET['find'] ?>" />
<input type="submit"
value="Find!" />
</form>
Student Exercise 11.3
Summary: Write a program that prints on
the bottom of the form, the same text
string that is contained in the text
widget. The string should be displayed
upon form submission.
...
</form>
<?php
if ( isset( $_REQUEST['find'] ) )
{
printf( "You submitted: \"%s\"",
$_REQUEST['find'] );
}
?>
</body>
</html>
Problem with Multiple Values (pg 1)
Problem: Checkboxes and section lists can have more than one value selected. A variable can
only store one value at a time. (see file “multiWrong.php”)
...
<?php
if ( $_GET )
{
printf( "<p /> <hr />" );
printf( "Your order is:<br />" );
<select name="widgetName"
multiple="multiple">
<option value="listItemValue">
List Item Label Here
</option>
...
</select>
Student Solution 11.4 (pg 1)
Here is one way to do it (see file “multiSelect.php”):
<br />
<p />
Fahrenheit To Centigrade:
5/9 * (Fahrenheit - 32)
Centigrade To Fahrenheit:
(9/5 * Centigrade) + 32
Centigrade To Kelvin:
Centigrade + 273
Kelvin To Centigrade:
Kelvin – 273
Fahrenheit To Kelvin:
5/9 * (Fahrenheit - 32) + 273
Kelvin To Fahrenheit:
((Kelvin - 273) * 9/5 ) + 32
Student Solution 11.4 (option 1, pg 1)
Here is one way to do it (see file “tempConvert.php”):