0% found this document useful (0 votes)
57 views29 pages

Introduction To PHP and Mysql: Working With Forms

The document provides information about working with forms in PHP and MySQL. It discusses several superglobal variables used to access form data, including $_GET, $_POST, and $_REQUEST. It presents examples of creating basic HTML and PHP forms, making forms "sticky" to remember previous values, and handling forms that allow multiple selections. Students are given exercises to display the contents of superglobal variables and modify example forms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views29 pages

Introduction To PHP and Mysql: Working With Forms

The document provides information about working with forms in PHP and MySQL. It discusses several superglobal variables used to access form data, including $_GET, $_POST, and $_REQUEST. It presents examples of creating basic HTML and PHP forms, making forms "sticky" to remember previous values, and handling forms that allow multiple selections. Students are given exercises to display the contents of superglobal variables and modify example forms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Introduction to

PHP and MySQL

Working With Forms


Superglobal Variable: $GLOBALS
Description: Superglobals are built-in variables that are global in scope.
These variables are available throughout the entire script.

$GLOBALS: Hash containing references to all variables defined in global


scope.

Example:
<?php
function initialize( $key, $value )
{
// define global variable
$GLOBALS[$key] = $value;
}

initialize( "foo", "It's every where!" );


echo( $GLOBALS["foo"] );
?>
Superglobal Variable: $_ENV
Description: The PHP mechanism for extracting operating system variables is
performed via $_ENV:

$_ENV: Hash containing system environment variables.

Example via “set” (Windows) or “printenv” (Unix):


“php.ini” directive concerning $_ENV
Note: If the array $_ENV is undefined, you may need
change the following setting in your “php.ini” file and
restart the web server. For xampp on windows, this
file is found in “C:\xampp\php\php.ini”
Check phpinfo() to verify the exact location of this file.

Change this line:


variables_order = "GPCS"

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.

Output should be similar to the following format:

$_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

foreach ( $env as $key => $val )


{
printf( "<b>\$_ENV[%s]:</b>
%s <p />\n",
$key, $val );
}
?>
Student Solution 11.1 (option 2)
Here is another way to do it (see file “_ENV.2.php”):

<?php
$env = $_ENV ; // copy original
ksort( $env ); // sort keys

foreach ( $env as $key => $val )


{
?>
<b>$_ENV[<?= $key ?>]:</b>
<?= $val ?> <p />
<?php
}
?>
“php.ini” directive concerning <?= ?>
Note: If “<?= ?>” is not working, you may need
change the following setting in your “php.ini” file and
restart the web server. For xampp on windows, this
file is found in “C:\xampp\php\php.ini”
Check the phpinfo() call for the exact location of this
file.

Change this line:


short_open_tag = Off

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

The following are some of the more significant keys


contained in this variable:

DOCUMENT_ROOT: Full pathname of the document


root on the server (example: /opt/lampp/htdocs)

HTTP_HOST: Server hostname (example: localhost)


SERVER_NAME: Server domain name (example:
www.example.com)
More Superglobal $_SERVER
HTTP_REFERER: Page previous to current page. Note:
Can not always be trusted.

HTTP_USER_AGENT: OS and browser of client (ex:


Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.5)
Gecko/20091109 Ubuntu/9.10 (karmic) Firefox/3.5.5)

PHP_SELF: Filename of current script. (ex:


/~frmcclurg/courses/php/examples/chapter11/_SERVE
R.php

QUERY_STRING: String containing the form variables


and values (ex: ?q=ubuntu)
Superglobal Variables for Forms
Description: The PHP mechanism for extracting
information from a form is performed via the
following predefined superglobal variables.

$_GET: Hash that is populated upon submitting a


form via the GET method.

$_POST: Hash that is populated upon submitting


a form via the POST method.

$_REQUEST: Hash that is populated with the


contents of $_GET and $_POST (and some
other variables).
Student Exercise 11.2 (extra credit)
Summary: Write a program that displays the key/value pairs contained in
the super global associative array “$_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.

Output should be in the following format:

$_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.

Solution: Any form element can become


“sticky” by using the results of the
previous submission via the $_GET,
$_POST, or $_REQUEST hash.
PHP Search Form Example
Description: Only a few changes may be necessary to convert a form
from HTML to PHP. The following code makes the form self processing
and “sticky” (see file “search.php”):

<form action="<?= $_SERVER['PHP_SELF'] ?>"


method="GET">

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.

Hint: The solution can be obtained by


adding less than a dozen lines to the file
“search.php”
Student Solution 11.3
The following lines should be added after the form in the file
“search.php” (see file “searchEcho.php”):

...
</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”)

<form action="<?= $PHP_SELF ?>" method="GET">


<input type="checkbox" name="food"
value="Hamburger" />
Hamburger <br />

<input type="checkbox" name="food" value="Fries" />


Fries <br />

<input type="checkbox" name="food" value="Drink" />


Drink <br />

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


</form>
...
Problem with Multiple Values (pg 2)
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 />" );

foreach ( $_GET as $name => $value )


{
printf( "\$_GET[%s]: %s <br />",
$name, $value );
}
}
?>
Solution with Multiple Values (pg 1)
Solution: An array is used to store multiple values. (see file “multiRight.php”)

<form action="<?= $PHP_SELF ?>" method="GET">


<input type="checkbox" name="food[]"
value="Hamburger" />
Hamburger <br />

<input type="checkbox" name="food[]"


value="Fries" />
Fries <br />

<input type="checkbox" name="food[]"


value="Drink" />
Drink <br />

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


</form>
Solution with Multiple Values (pg 2)
Solution: An array is used to store multiple values. (see file “multiRight.php”)
<?php
if ( $_GET['food'] )
{
printf( "<p /> <hr />" );
printf( "Your order:<br />" );

for ( $i = 0; $i < count( $_GET['food'] ); $i++)


{
$value = $_GET['food'][$i];

printf( "\$_GET['food'][%d]: %s <br />",


$i, $value );
}
}
?>
Student Exercise 11.4 (pg 1)
Step 1: Create a form that contains a multiple
selection list box and one checkbox similar to
the following:
Student Exercise 11.4 (pg 2)
Step 2: Write a program that will display all the key/value
pairs contained in the super global associative array
“$_REQUEST” upon pressing the submit button.

Hint: The syntax for a selection box is as follows:

<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”):

<form action="<?= $PHP_SELF ?>" method="GET">


<input type="checkbox" name="used" value="1"> Used?

<br />

<select name="media[]" multiple="multiple">


<option value="Hardback"> Hard Cover </option>
<option value="Paperback"> Soft Cover </option>
<option value="CD"> Audio Book </option>
<option value="DVD"> Video </option>
</select>

<p />

<input type="submit" name="order" value="Order" />


</form>
Student Solution 11.4 (pg 2)
Here is one way to do it (for a complete file listing, see file “multiSelect.php”):
<?php
if ( isset( $_REQUEST['order'] ) ) {
printf( "<p /> <hr /> You selected:<br />\n" );
foreach ( $_REQUEST as $key => $outie ) {
if ( is_array( $_REQUEST[$key] ) ) {
$i = 0; // reset counter
foreach ( $_REQUEST[$key] as $innie ) {
printf( "<dd> \$_REQUEST[%s][%d]: %s<br />\n",
$key, $i, $innie);
$i++;
} // end foreach
} // end if
else {
printf( "\$_REQUEST[%s]: %s<br />\n", $key, $outie);
} // end else
} // end foreach
} // end if
?>
Student Exercise 11.4
Summary: Select one formula below. Create a form that allows a user to type
the text value of a temperature and output its equivalent value in another
unit upon form submission. Extra credit: Select between multiple
conversions using a radio button.

The conversion formulas are as follows:

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”):

<form action="<?= $PHP_SELF ?>"


method="GET">
<input type="text" name="degrees"
value="<?= $_GET['degrees'] ?
>" />
<br />
<input type="submit" name="doit"
value="Convert" />
</form>
...
Student Solution 11.4 (option 1, pg 2)
Here is one way to do it (see file “tempConvert.php”):
...
<?php
if ( isset( $_GET['doit'] ) )
{
printf( "<p /> <hr />\n" );
$input = $_GET['degrees'];
/* Fahrenheit To Centigrade */
$output = 5/9 * ($input - 32);
$inputUnits = "Fahrenheit";
$outputUnits = "Centigrade";
printf( "%f&deg; %s = %f&deg; %s",
$input, $inputUnits,
$output, $outputUnits );
}
?>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy