Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

overwrite the value of the first. Other CGI solutions might create an array in this
situation. If you wish to pass arrays through form fields, you can define form fields with
square brackets. This issue is dealt with in more detail in later chapters.


Listing 7.3 is an example of using variables created from form fields. The script expects a
variable named inputColor. The first time this page is viewed, inputColor will be
empty, so the script sets it to be six Fs, the RGB code for pure white. On subsequent calls
to the page, the value of the text box will be used to set the background color of the page.
Notice that inputColor is also used in the INPUT field to prepopulate it. This way, each
time you submit the form, you remember what you entered. As an aside, you should also
take note of the technique used here, in which a page calls itself.


Listing 7.3 Getting Form Input


<?
print("\n");
print("\n");
print("Listing 7.3\n");
print("\n");


/
if here for the first time
use white for bgcolor
/
if($inputColor == "")
{
$inputColor = "FFFFFF";
}


/*
* open body with background color
/
print("<BODY BGCOLOR=\"#$inputColor\">\n");


/*
* start form, action is this page itself
/
print("<FORM ACTION=\"$PHP_SELF\" METHOD=\"post\">\n");


/*
* get color
/
print("Enter HTML color: ");
print("<INPUT ");
print("TYPE=\"text\" ");
print("NAME=\"inputColor\" ");
print("VALUE=\"$inputColor\">\n");


/*
* show submit button
/
print("<INPUT ");
print("TYPE=\"submit\" ");
print("NAME=\"Submit_Button\" ");

Free download pdf