Getting Input from Forms PHP

Sending text to the browser is easy to understand. Getting input from forms is a little tricky. HTML offers several ways to get information from the user via forms. There are text fields, text areas, selection lists, and radio buttons among others. Each of these becomes a string of text offered to the Web server when the user clicks the submit button.

When a form is submitted, PHP turns each form field into a variable. The variables created this way are like any other variable. You may even change their values. They are created s if you had written the PHP code to put values into the variables. This means that if you put wo form variables on a page with the same name, the second one may overwrite the value of theirst. Other CGI solutions might create an array in this situation. If you wish to pass arrays hrough form fields, you can define form fields with square brackets.

Listing 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.

Getting Form Input

<?
print("<HTML> ");
print("<HEAD> ");
print("<TITLE>Listing 7.3</TITLE> ");
print("</HEAD> ");
/*
** if here for the first time
** use white for bgcolor
*/
if($inputColor == "")
{
$inputColor = "FFFFFF";
}
/*
** open body with background color
*/
print("<BODY BGCOLOR="#$inputColor"> ");
/*
** start form, action is this page itself
*/
print("<FORM ACTION="$PHP_SELF" METHOD="post"> ");
/*
** get color
*/
print("<B>Enter HTML color:</B> ");
print("<INPUT ");
print("TYPE="text" ");
print("NAME="inputColor" ");
print("VALUE="$inputColor"> ");
/*
** show submit button
*/
print("<INPUT ");
print("TYPE="submit" ");
print("NAME="Submit_Button" ");
print("VALUE="Try It"> ");
print("</FORM> ");
print("</BODY> ");
print("</HTML> ");
?>


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics