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

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

Текущая версия на 07:04, 26 мая 2010

Saving State with a Hidden Field

 
<?php
  $num_to_guess = 42;
  $message = "";
  if (! isset ( $_POST ["guess"] )) {
    $message = "Welcome!";
  } else if ($_POST ["guess"] > $num_to_guess) {
    $message = $_POST ["guess"] . " is too big!";
  } else if ($_POST ["guess"] < $num_to_guess) {
    $message = $_POST ["guess"] . " is too small!";
  } else {
    $message = "Well done!";
  }
  $guess = ( int ) $_POST ["guess"];
  $num_tries = ( int ) $_POST ["num_tries"];
  $num_tries ++;
  ?>
<html>
<head>
<title>A PHP Number Guessing Script</title>
</head>
<body>
 <?php print $message?>
 Guess number: <?php print $num_tries?><br />
<form method="post" action="<?php
print $_SERVER ["PHP_SELF"]?>">
<input type="hidden" name="num_tries"
  value="<?php
  print $num_tries?>" /> Type your guess here: <input type="text" name="guess" value="<?php
  print $guess?>" />
</form>
</body>
</html>



Using a hidden parameter to indicate form submission

 
<?
if ($_POST["_submit_check"]) {
    process_form();
} else {
    show_form();
}
function process_form() {
    print "Hello, ". $_POST["my_name"];
}
function show_form() {
    print<<<_HTML_
<form method="POST" action="$_SERVER[PHP_SELF]">
Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
?>