PHP/Form/Form Post — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 10:37, 26 мая 2010

An HTML Form That Calls Itself

 
<html>
<head>
<title>An HTML Form that Calls Itself</title>
</head>
<body>
<div>
<?php
if ( ! empty( $_POST["guess"] ) ) {
    print "last guess: ".$_POST["guess"];
}
?>
<form method="post" action="<?php print $_SERVER["PHP_SELF"]?>">
<p>
Type your guess here: <input type="text" name="guess" />
</p>
</form>
</div>
</body>
</html>



Combined Feedback Form

 
<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><br/>";
$form.="<input type=\"submit\" name=\"sent\" value=\"Send Form\">";
$form.="</form>";
if($sent)
{
  $valid=true;
  if( !$username )
  { $errmsg.="Enter your name...<br />"; $valid = false; }
  if( !$useraddr )
  { $errmsg .="Enter your email address...<br />"; $valid = false; }
  if( !$comments )
  { $errmsg.="Enter your comments...<br />"; $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!<br />";
    $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>



Form submitting

 
<?php
    if (isset($_POST["submit"])){
        echo "Hi ".$_POST["name"]."!<br />";
        echo "The address ".$_POST["email"]." will soon be a spam-magnet!<br />";
    }
?>
<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>



GET vs. POST

 
<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.";
        }
        ?><br /><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">
          <p>GET 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 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>



Making a multipage form

 
<?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"/> <br/>
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"/> <br/>
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 } ?>



One-script form processing

 
//File: index.php
<html>
<head>
<title></title>
</head>
<body>
<?
$form = "<form action=\"index.php\" method=\"post\">
<input type=\"hidden\" name=\"seenform\" value=\"y\">
<b>Give us some information!</b><br>
Your Name:<br>
<input type=\"text\" name=\"name\" size=\"20\" maxlength=\"20\" value=\"\"><br>
Your Email:<br>
<input type=\"text\" name=\"email\" size=\"20\" maxlength=\"40\" value=\"\"><br>
<input type=\"submit\" value=\"subscribe!\">
</form>";
if ($seenform != "y"):
     print "$form";
else :
     print "Hi, $name!. Your email address is $email";
endif;
?>
</body>
</html>



Preventing Multiple Submissions on the Client Side

 
<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"];
?>



Preventing Multiple Submissions on the Server Side

 
<html>
<body>
<form action="index.php" method="post">
<input type="hidden" name="submitted" value="yes" /> Your Name: <input
  type="text" name="yourname" maxlength="150" /><br />
<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"] );
}
?>