PHP/Form/Form
Содержание
HTML elements for use in forms
<source lang="html4strict">
Element Description
input type="checkbox" A checkbox that lets users select multiple options.
input type="file" A text box plus a button that opens a file selection dialog.
input type="hidden" A hidden form element where you set the value.
input type="password" A text box where the text is replaced by a password character (usually asterisk *).
input type="radio" A radio button. Radio buttons are like grouped checkboxesyou can only select one at a time.
input type="reset" A button to clear the form. It"s one of the weird oddities of the Web that this still existsdo you know anyone who uses it?
input type="submit" A button to submit the form.
input type="text" A text box.
option An option in a SELECT element.
select A listbox; can also be a drop-down list box.
textarea Multiline text box.
</source>
HTML Form Elements
<source lang="html4strict">
Element Description TEXT INPUT A simple text box PASSWORD INPUT A text box that hides the characters inputted HIDDEN INPUT A field that does not show on the form but can contain data SELECT A drop-down box with options LIST A select box that can have multiple options selected CHECKBOX A box that can be checked RADIO A radio button that can act as a choice TEXTAREA A larger box that can contain paragraph-style entries FILE An element that allows you to browse your computer for a file SUBMIT A button that will submit the form RESET A button that will reset the form to its original state <html> <body> <?php if ($_GET ["submitted"] == "yes") {
if (trim ( $_GET ["yourname"] ) != "") { echo "Your Name (with GET): " . $_GET ["yourname"]; } else { echo "You must submit a value."; } ?>
<a href="index.php">Try Again</a> <?php } if ($_POST ["submitted"] == "yes") {
if (trim ( $_POST ["yourname"] ) != "") { echo "Your Name (with POST): " . $_POST ["yourname"]; } else { echo "You must submit a value."; } ?>
<a href="index.php">Try Again</a><?php } ?> <?php if ($_GET ["submitted"] != "yes" && $_POST ["submitted"] != "yes") {
?>
<form action="index.php" method="get"> GET Example: <input type="hidden" name="submitted" value="yes" /> Your Name: <input
type="text" name="yourname" maxlength="150" />
<input type="submit" value="Submit with GET"/> </form> <form action="index.php" method="post">
POST Example:
<input type="hidden" name="submitted" value="yes" /> Your Name: <input
type="text" name="yourname" maxlength="150" />
<input type="submit" value="Submit with POST" /> </form> <?php } ?> </body> </html>
</source>
HTML form for submitting data
<source lang="html4strict">
<form method="POST" action="index.php">
Your Name: <input type="text" name="user">
<input type="submit" value="Say Hello">
</form>
//Dynamic data //index.php <?php print "Hello, "; // Print what was submitted in the form parameter called "user" print $_POST["user"]; print "!"; ?>
</source>
print a radio button or checkbox
<source lang="html4strict">
function input_radiocheck($type, $element_name, $values, $element_value) {
print "<input type="" . $type . "" name="" . $element_name ."" value="" . $element_value . "" "; if ($element_value == $values[$element_name]) { print " checked="checked""; } print "/>";
}
</source>
<source lang="html4strict">
function input_select($element_name, $selected, $options, $multiple = false) {
print "<select name="" . $element_name; if ($multiple) { print "[]" multiple="multiple"; } print "">"; $selected_options = array(); if ($multiple) { foreach ($selected[$element_name] as $val) { $selected_options[$val] = true; } } else { $selected_options[ $selected[$element_name] ] = true; } foreach ($options as $option => $label) { print "<option value="" . htmlentities($option) . """; if ($selected_options[$option]) { print " selected="selected""; } print ">" . htmlentities($label) . "</option>"; } print "</select>";
}
</source>
print a submit button
<source lang="html4strict">
function input_submit($element_name, $label) {
print "<input type="submit" name="" . $element_name ."" value=""; print htmlentities($label) .""/>";
}
</source>
print a textarea
<source lang="html4strict">
function input_textarea($element_name, $values) {
print "<textarea name="" . $element_name ."">"; print htmlentities($values[$element_name]) . "</textarea>";
}
</source>
print a text box
<source lang="html4strict">
function input_text($element_name, $values) {
print "<input type="text" name="" . $element_name ."" value=""; print htmlentities($values[$element_name]) . "">";
}
</source>
Process the results
<source lang="html4strict">
<html> <head>
<title>Building a Form</title>
</head> <body> <?php $search = htmlentities($_GET["search"]); $self = htmlentities($_SERVER["PHP_SELF"]); if ($search ==="" ){
echo (" <form action="".$self."" method="GET"> <label>Search: <input type="text" name="search" /></label> <input type="submit" value="Go!" /> </form>");
} else {
echo "The search string is: $search</string>";
} ?> </body> </html>
</source>
Working with Multipage Forms
<source lang="html4strict">
<html> <body>
<form action="page2.php" method="post">
Page 1 Data Collection:
<input type="hidden" name="submitted" value="yes" />
Your Name: <input type="text" name="yourname" maxlength="150" />
<input type="submit" value="Submit" style="margin-top: 10px;" />
</form>
</body> </html>
<html> <body>
<form action="page3.php" method="post">
Page 2 Data Collection:
Selection: <select name="yourselection"> <option value="nogo">make a selection...</option> <option value="1">Choice 1</option> <option value="2">Choice 2</option> <option value="3">Choice 3</option> </select> <input type="hidden" name="yourname" . value="<?php echo $_POST["yourname"]; ?>" /> <input type="submit" value="Submit" style="margin-top: 10px;" /> </form>
</body> </html>
<html> <body> <form action="page4.php" method="post">
Page 3 Data Collection:
Your Email: <input type="text" name="youremail" maxlength="150" />
<input type="hidden" name="yourname".
value="<?php echo $_POST["yourname"]; ?>" />
<input type="hidden" name="yourselection".
value="<?php echo _POST["yourselection"]; ?>" />
<input type="submit" value="Submit"/>
</form>
</body>
</html>
<html>
<body>
<?php
echo "Your Name: " . $_POST["yourname"] . "
";
echo "Your Selection: " . $_POST["yourselection"] . "
";
echo "Your Email: " . $_POST["youremail"] . "
";
?>
<a href="page1.php">Try Again</a>
</body>
</html>
</source>