PHP/Form/Form Data
Содержание
Accessing multiple submitted values
<source lang="html4strict">
<form method="POST" action="index.php">
<select name="lunch[]" multiple>
<option value="a">A</option>
<option value="b">B</option>
<option value="c">C</option>
<option value="d">D</option>
<option value="e">E</option>
</select>
<input type="submit" name="submit">
</form>
Selected buns:
<?php
foreach ($_POST["lunch"] as $choice) {
print "You want a $choice bun.
";
} ?>
</source>
Access widget"s form value
<source lang="html4strict">
<INPUT TYPE="text" NAME="myform.email">
would be accessed in PHP as the following: <?php
echo $_GET["myform_email"];
?>
</source>
A process_form() Function
<source lang="html4strict">
<?php
function process_form($data) { $msg = "The form at {$_SERVER["PHP_SELF"]} was submitted with these values: \n\n"; foreach($data as $key=>$val) { $msg .= "$key => $val\n"; } mail("joeuser@somewhere.ru", "form submission", $msg); }
?>
</source>
Calculation based on form data
<source lang="html4strict">
<html>
<head> <title>Calculation Form</title> </head> <body> <form action = "calc.php" method = "post"> Value 1: <input type = "text" name = "val1" size = "10"> Value 2: <input type = "text" name = "val2" size = "10">
Calculation:
<input type = "radio" name = "calc" value = "add"> Add <input type = "radio" name = "calc" value = "sub"> Subtract <input type = "radio" name = "calc" value = "mul"> Multiply <input type = "radio" name = "calc" value = "div"> Divide
<input type = "submit" value = "Calculate"> <input type = "reset" value = "Clear"> </form> </body>
</html>
File: calc.php
<html>
<head> <title>Calculation Result</title> </head> <body> <?php $val1 = $_POST["val1"]; $val2 = $_POST["val2"]; $calc = $_POST["calc"]; if( is_numeric( $val1 ) && is_numeric( $val2 ) ) { if( $calc != null ) { switch( $calc ) { case "add" : $result = $val1 + $val2; break; case "sub" : $result = $val1 - $val2; break; case "mul" : $result = $val1 * $val2; break; case "div" : $result = $val1 / $val2; break; } echo( "Calculation result: $result" ); } } else { echo( "Invalid entry - please retry" ); } ?> </body>
</html>
</source>
Deal with Array Form Data
<source lang="html4strict">
<HTML> <BODY> <FORM METHOD="POST" ACTION="DealWithArrayFormData.php">
Contact Information
Childrens" Names: |
<INPUT TYPE="TEXT" NAME="children[]"> |
<INPUT TYPE="TEXT" NAME="children[]"> |
<INPUT TYPE="TEXT" NAME="children[]"> |
<INPUT TYPE="TEXT" NAME="children[]"> |
<INPUT TYPE="TEXT" NAME="children[]"> |
<INPUT TYPE="SUBMIT" VALUE="Submit">
<INPUT TYPE="RESET" VALUE="Clear the Form">
</FORM>
</BODY>
</HTML>
</source>
Feedback Form
<source lang="html4strict">
<html>
<head> <title>Feedback Form</title> </head> <body> <form action="feedback.php" method="post"> Name:<input type="text" name="username" size="30">
Email:<input type="text" name="useraddr" size="30">
<textarea name="comments" cols="30" rows="5"> </textarea>
<input type="submit" value="Send Form"> </form> </body>
</html>
File: feedback.php
<?php $username = $_POST["username"]; $useraddr = $_POST["useraddr"]; $comments = $_POST["comments"]; $to = "php@h.ru"; $re = "Website Feedback"; $msg = $comments; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; $headers .= "From: $useraddr \r\n"; $headers .= "Cc: another@hotmail.ru \r\n"; mail( $to, $re, $msg, $headers ); ?> <html>
<head> <title>Message Received</title> </head> <body>
Thanks for your comments
Message received from <?php echo($username); ?>
Reply to <?php echo($useraddr); ?> </body>
</html>
</source>
Forms and PHP
<source lang="html4strict">
//index.htm
<html>
<body>
<form action="index.php" method="post">
Your Name:
<input type="text" name="name" size="20" maxlength="20" value="">
Your Email:
<input type="text" name="email" size="20" maxlength="40" value="">
<input type="submit" value="go!">
</form>
</body>
</html>
//index.php <html> <head> <title></title> </head> <body> <?
print "Hi, $name!. Your email address is $email";
?> </body> </html>
</source>
Handling Special Characters
<source lang="html4strict">
<html> <body>
<?php if ($_POST["submitted"] == "yes"){ $yourname = $_POST["yourname"]; $yourname = trim ($yourname); $yourname = strip_tags ($yourname); $yourname = htmlspecialchars ($yourname); $yourname = addslashes ($yourname); echo $yourname . "
"; ?><a href="index.php">Try Again</a><?php } if ($_POST["submitted"] != "yes"){ ?> <form action="index.php" method="post">
Example:
<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> <?php } ?> </div>
</body> </html>
</source>
multivalue-forms.php
<source lang="html4strict">
<?php
if (isset($_POST["submit"])) { echo "You like the following languages:
"; foreach($_POST["languages"] AS $language) echo "$language
"; }
?> <form action="index.php" method="post">
Check all that apply): <input type="checkbox" name="languages[]" value="csharp" />C#
<input type="checkbox" name="languages[]" value="jscript" />JavaScript
<input type="checkbox" name="languages[]" value="perl" />Perl
<input type="checkbox" name="languages[]" value="php" />PHP
<input type="submit" name="submit" value="Go!" />
</form>
</source>
Saying "Hello"
<source lang="html4strict">
<? if (array_key_exists("my_name",$_POST)) {
print "Hello, ". $_POST["my_name"];
} else {
print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>
</source>