PHP/Form/Form — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
| |
Версия 10:37, 26 мая 2010
Содержание
HTML elements for use in forms
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.
HTML Form Elements
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.";
}
?><br />
<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" /><br />
<input type="submit" value="Submit with GET"/>
</form>
<form action="index.php" method="post">
<p>POST Example:</p>
<input type="hidden" name="submitted" value="yes" /> Your Name: <input
type="text" name="yourname" maxlength="150" /><br />
<input type="submit" value="Submit with POST" />
</form>
<?php
}
?>
</body>
</html>
HTML form for submitting data
<form method="POST" action="index.php">
Your Name: <input type="text" name="user">
<br/>
<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 "!";
?>
print a radio button or checkbox
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 "/>";
}
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>";
}
print a submit button
function input_submit($element_name, $label) {
print "<input type="submit" name="" . $element_name ."" value="";
print htmlentities($label) .""/>";
}
print a textarea
function input_textarea($element_name, $values) {
print "<textarea name="" . $element_name ."">";
print htmlentities($values[$element_name]) . "</textarea>";
}
print a text box
function input_text($element_name, $values) {
print "<input type="text" name="" . $element_name ."" value="";
print htmlentities($values[$element_name]) . "">";
}
Process the results
<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: <strong>$search</string>";
}
?>
</body>
</html>
Working with Multipage Forms
<html>
<body>
<div>
<form action="page2.php" method="post">
<p>Page 1 Data Collection:</p>
<input type="hidden" name="submitted" value="yes" />
Your Name: <input type="text" name="yourname" maxlength="150" /><br /><br />
<input type="submit" value="Submit" style="margin-top: 10px;" />
</form>
</div>
</body>
</html>
<html>
<body>
<div>
<form action="page3.php" method="post">
<p>Page 2 Data Collection:</p>
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>
</div>
</body>
</html>
<html>
<body>
<form action="page4.php" method="post">
<p>Page 3 Data Collection:</p>
Your Email: <input type="text" name="youremail" maxlength="150" /><br />
<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"] . "<br />";
echo "Your Selection: " . $_POST["yourselection"] . "<br />";
echo "Your Email: " . $_POST["youremail"] . "<br />";
?>
<a href="page1.php">Try Again</a>
</body>
</html>