PHP/Form/Form Post
Содержание
An HTML Form That Calls Itself
<source lang="html4strict">
<html> <head> <title>An HTML Form that Calls Itself</title> </head> <body>
<?php if ( ! empty( $_POST["guess"] ) ) {
print "last guess: ".$_POST["guess"];
} ?> <form method="post" action="<?php print $_SERVER["PHP_SELF"]?>">
Type your guess here: <input type="text" name="guess" />
</form>
</body> </html>
</source>
Combined Feedback Form
<source lang="html4strict">
<html>
<head><title>Combined Feedback Form</title></head>
<body>
<?php
$self = $_SERVER["PHP_SELF"];
$username = $_POST["username"];
$useraddr = $_POST["useraddr"];
$comments = $_POST["comments"];
$sent = $_POST["sent"];
$form ="<form action=\"$self\" method=\"post\">";
$form.="Name:<input type=\"text\" name=\"username\"";
$form.=" size=\"30\" value=\"$username\" >";
$form.="Email:<input type=\"text\" name=\"useraddr\"";
$form.=" size=\"30\" value=\"$useraddr\">";
$form.="Comments:<textarea name=\"comments\" >";
$form.="$comments</textarea>
";
$form.="<input type=\"submit\" name=\"sent\" value=\"Send Form\">";
$form.="</form>";
if($sent)
{
$valid=true; if( !$username ) { $errmsg.="Enter your name...
"; $valid = false; } if( !$useraddr ) { $errmsg .="Enter your email address...
"; $valid = false; } if( !$comments ) { $errmsg.="Enter your comments...
"; $valid = false; } $useraddr = trim($useraddr); $_name = "/^[-!#$%&\"*+\\.\/0-9=?A-Z^_"{|}~]+"; $_host = "([-0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if( !preg_match( $_name."@".$_host .$_tlds,$useraddr ) ) { $errmsg.="Email address has incorrect format!
"; $valid=false; }
} if($valid != true) {
echo( $errmsg.$form );
} else {
$to = "php@h.ru"; $re = "Feedback from $username"; $msg = $comments; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html;"; $headers .= "charset=\"iso-8859-1\"\r\n"; $headers .= "From: $useraddr \r\n"; if(mail($to,$re,$msg, $headers)) { echo("Your comments have been sent - thanks $username");}
} ?> </body></html>
</source>
Form submitting
<source lang="html4strict">
<?php
if (isset($_POST["submit"])){ echo "Hi ".$_POST["name"]."!
"; echo "The address ".$_POST["email"]." will soon be a spam-magnet!
"; }
?> <form action="index.php" method="post"> Name:<input type="text" name="name" size="20" maxlength="40" value="" /> Email Address: <input type="text" name="email" size="20" maxlength="40" value="" /> <input type="submit" name = "submit" value="Go!" /> </form>
</source>
GET vs. POST
<source lang="html4strict">
<html> </head> <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>
Making a multipage form
<source lang="html4strict">
<?php session_start(); if (($_SERVER["REQUEST_METHOD"] == "GET") || (! isset($_POST["stage"]))) {
$stage = 1;
} else {
$stage = (int) $_POST["stage"];
} if ($stage > 1) {
foreach ($_POST as $key => $value) { $_SESSION[$key] = $value; }
} if ($stage == 1) { ?>
<form action="<?php echo $_SERVER["SCRIPT_NAME"] ?>" method="post">
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/> </br/>
<input type="hidden" name="stage" value="<?php echo $stage + 1 ?>"/>
<input type="submit" value="Next"/>
</form>
<?php } else if ($stage == 2) { ?>
<form action="<?php echo $_SERVER["SCRIPT_NAME"] ?>" method="post">
Favorite Color: <input type="text" name="color"/>
Favorite Food: <input type="text" name="food"/> </br/>
<input type="hidden" name="stage" value="<?php echo $stage + 1 ?>"/> <input type="submit" value="Done"/> </form> <?php } else if ($stage == 3) { ?>
Hello <?php echo $_SESSION["name"] ?>. You are <?php echo $_SESSION["age"] ?> years old. Your favorite color is <?php echo $_SESSION["color"] ?> and your favorite food is <?php echo $_SESSION["food"] ?>.
<?php } ?>
</source>
One-script form processing
<source lang="html4strict">
//File: index.php
<html>
<head>
<title></title>
</head>
<body>
<?
$form = "<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
Give us some information!
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=\"subscribe!\">
</form>";
if ($seenform != "y"):
print "$form";
else :
print "Hi, $name!. Your email address is $email";
endif; ?> </body> </html>
</source>
Preventing Multiple Submissions on the Client Side
<source lang="html4strict">
<html> <script language="javascript" type="text/javascript"> function checkandsubmit() {
document.test.submitbut.disabled = true; document.test.submit();
} </script> <body> <form action="index.php" method="post" name="test" onsubmit="return checkandsubmit ()"> <input type="hidden" name="submitted" value="yes" /> Your Name: <input
type="text" name="yourname" maxlength="150" />
<input type="submit" value="Submit" id="submitbut" name"submitbut" /></form> </body> </html> <?php if ($file = fopen ( "test.txt", "w+" )) {
fwrite ( $file, "Processing" );
} else {
echo "Error opening file.";
} echo $_POST ["yourname"]; ?>
</source>
Preventing Multiple Submissions on the Server Side
<source lang="html4strict">
<html> <body> <form action="index.php" method="post"> <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> <?php session_start (); if (! isset ( $_SESSION ["processing"] )) {
$_SESSION ["processing"] = false;
} if ($_SESSION ["processing"] == false) {
$_SESSION ["processing"] = true; //validation if ($file = fopen ( "test.txt", "w+" )) { fwrite ( $file, "Processing" ); } else { echo "Error opening file."; } echo $_POST ["yourname"]; unset ( $_SESSION ["processing"] );
} ?>
</source>