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

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

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

A day choice <select> menu

 
<?
$midnight_today = mktime(0,0,0);
print "<select name="date">";
for ($i = 0; $i < 7; $i++) {
    $timestamp = strtotime("+$i day", $midnight_today);
    $display_date = strftime("%A, %B %d, %Y", $timestamp);
    print "<option value="" . $timestamp ."">".$display_date."</option>\n";
}
print "\n</select>";
?>



An HTML Form Including a SELECT Element

<html>
<head>
<title>An HTML form including a SELECT element</title>
</head>
<body>
<form action="formSelectData.php" method="POST">
  <input type="text" name="user">
  <br>
  <textarea name="address" rows="5" cols="40"></textarea>
  <br>
  <select name="products[]" multiple>
    <option>option1
    <option>option2
    <option>option3
    <option>option4
  </select>
  <br>
  <input type="submit" value="OK">
</form>
</body>
</html>
<!-- formSelectData.php
<html>
<head>
<title>Reading input from the form</title>
</head>
<body>
<?php
print "Welcome <b>$user</b><p>\n\n";
print "Your address is:<p>\n\n<b>$address</b><p>\n\n";
print "Your product choices are:<p>\n\n";
print "<ul>\n\n";
foreach ( $products as $value ){
    print "<li>$value<br>\n";
}
print "</ul>";
?>
</body>
</html>

-->



An HTML Form with a select Element

 
<html>
<head>
<title>An HTML Form with a "select" Element</title>
</head>
<body>
<div>
<form action="index.php" method="post">
<p><input type="text" name="user" /></p>
<p><textarea name="address" rows="5" cols="40">
 </textarea></p>
<p><select name="products[]" multiple="multiple">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select></p>
<p><input type="submit" value="hit it!" /></p>
</form>
</div>
</body>
</html>
//Reading Input from the Form
<html>
<head>
<title>Reading Input from the Form</title>
</head>
<body>
<div>
 <?php
  print "Welcome <b>" . $_POST ["user"] . "</b><br/>";
  print "Your address is:<br/><b>" . $_POST ["address"] . "</b><br/>\n";
  
  if (is_array ( $_POST ["products"] )) {
    print "<p>Your product choices are:</p>";
    print "<ul>";
    foreach ( $_POST ["products"] as $value ) {
      print "<li>$value</li>\n";
    }
    print "</ul>";
  }
  ?>
 </div>
</body>
</html>



A <select> menu with different choices and values

 
<?
$sweets = array("puff" => "A",
                "square" => "C",
                "cake" => "B",
                "ricemeat" => "S");
function show_form() {
    print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Order: <select name="order">
_HTML_;
foreach ($GLOBALS["sweets"] as $val => $choice) {
    print "<option value=\"$val\">$choice</option>\n";
}
print<<<_HTML_
</select>
<br/>
<input type="submit" value="Order">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
?>



Creating Form Elements Based on the Current Time and/or Date

 
<html>
<body>
    <?php
      if ($_POST["submitted"] == "yes"){
        echo $_POST["month"] . "/" . $_POST["day"] . "/" . $_POST["year"] . " - " . $_POST["hour"] . ":" . $_POST["minute"] . ":" . $_POST["second"];
        ?><br /><a href="index.php">Try Again</a><?php
      }
      if ($_POST["submitted"] != "yes"){
        ?>
        <form action="index.php" method="post">
          <p>Example:</p>
          <input type="hidden" name="submitted" value="yes" />
          Select a Date and Time: <br />
          <select name="month">
            <?php
              for ($i = 1; $i <= 12; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("n")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> / 
          <select name="day">
            <?php
              for ($i = 1; $i <= 31; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("j")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> / 
          <select name="year">
            <?php
              for ($i = 1950; $i <= date ("Y"); $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("Y")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> - 
          <select name="hour">
            <?php
              for ($i = 1; $i <= 24; $i++){
                ?><option value="<?php echo $i; ?>"<?php if ($i == date ("G")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> : 
          <select name="minute">
            <?php
              for ($i = 1; $i <= 60; $i++){
                //Deal with leading zeros.
                if ($i < 10){
                  $comparem = "0" . $i;
                } else {
                  $comparem = $i;
                }
                ?><option value="<?php echo $i; ?>"<?php if ($comparem == date ("i")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select> : 
          <select name="second">
            <?php
              for ($i = 1; $i <= 60; $i++){
                if ($i < 10){
                  $compares = "0" . $i;
                } else {
                  $compares = $i;
                }
                ?><option value="<?php echo $i; ?>"<?php if ($compares == date ("s")){?> selected="selected"<?php } ?>><?php echo $i; ?></option><?php
              }
            ?>
          </select>
          <br /><input type="submit" value="Submit" style="margin-top: 10px;" />
        </form>
        <?php
      }
    ?>
  </div>
</body>
</html>



Creating Form Elements with Multiple Options

 
<html>
<body> 
<?php
if ($_POST ["submitted"] == "yes") {
  if (count ( $_POST ["fruit"] ) != 0) {
    echo "Your Selections:<br />";
  } else {
    echo "You have not made any selections.<br /><br />";
  }
  for($i = 0; $i < count ( $_POST ["fruit"] ); $i ++) {
    echo $_POST ["fruit"] [$i] . "<br />";
  }
  ?><a href="index.php">Try Again</a><?php
}
if ($_POST ["submitted"] != "yes") {
?> 
<form action="index.php" method="post">
<input type="hidden" name="submitted" value="yes" /> Your Choice (s): <br />
<select name="fruit[]" multiple="multiple">
  <option value="Bananas">Bananas</option>
  <option value="Apples">Apples</option>
  <option value="Oranges">Oranges</option>
</select><br />
<input type="submit" value="Submit" /></form> 
<?php
}
?> 
</body>
</html>



Displaying a <select> menu

 
<?
$sweets = array("A","B","C","D");
function show_form() {
    print<<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Order: <select name="order">
_HTML_;
foreach ($GLOBALS["sweets"] as $choice) {
    print "<option>$choice</option>\n";
}
print<<<_HTML_
</select>
<br/>
<input type="submit" value="Order">
<input type="hidden" name="_submit_check" value="1">
</form>
_HTML_;
}
?>



Form select input

       
       
<HTML>
<HEAD>
<TITLE>Candy preference form</TITLE>
</HEAD>
   
<BODY>
<FORM ACTION="SelectFormControlHandler.php" METHOD="POST">
What"s your most favorite kind of candy?<BR>
<INPUT TYPE="radio" NAME="Candy" VALUE="peanut butter cups">Peanut butter cups<BR>
<INPUT TYPE="radio" NAME="Candy" VALUE="Snickers">Snickers<BR>
<INPUT TYPE="radio" NAME="Candy" VALUE="Turtles">Turtles<BR>
<INPUT TYPE="submit">
</FORM>
</BODY>
</HTML>

<!-- SelectFormControlHandler.php
<HTML>
<HEAD>
<TITLE>Candy preference reply</TITLE>
</HEAD>
   
<BODY>
Yum, <?php print("$Candy!  ");
if($Candy == "peanut butter cups"){
  print("peanut butter cups");
  print(" $Candy.");
}else{
  print("$Candy");
  if($Candy == "Snickers"){
    print("Snickers");
  }elseif($Candy == "Turtles"){
    print("Turtles");
  }
}
?>
</BODY>
</HTML>
-->



Generating a dynamic pull-down menu

 
<?
if ($site != "") :
    header("Location: http://$site");
    exit;
else :
?>
<html>
<head>
<title></title>
</head>
<body>
<?
$favsites = array ("www.wbex.ru","www.yahoo.ru","www.ms.ru","www.php.ru");
?>
<form action = "index.php" method="post">
<select name="site">
<option value = "">Choose a site:
<?
$x = 0;
while ( $x < sizeof ($favsites) ) :
          print "<option value="$favsites[$x]">$favsites[$x]";
          $x++;
endwhile;
?>
</select>
<input type="submit" value="go!">
</form>
<?
endif;
?>



One choice for each day from 1 to 31

 
<?
$months = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 
                5 => "May", 6 => "June", 7 => "July", 8 => "August",
                9 => "September", 10 => "October", 11 => "November", 
                12 => "December");
print "<select name="day">";
for ($i = 1; $i <= 31; $i++) {
    print "<option value="" . $i . "">" . $i ."</option>\n";
}
print "</select> \n";
?>



One choice for each year from last year to five years from now

 
<?
$months = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 
                5 => "May", 6 => "June", 7 => "July", 8 => "August",
                9 => "September", 10 => "October", 11 => "November", 
                12 => "December");
print "<select name="year">";
for ($year = date("Y") -1, $max_year = date("Y") + 5; $year < $max_year; $year++) {
    print "<option value="" . $year . "">" . $year ."</option>\n";
}
print "</select> \n";
?>



Setting a default value in a <select> menu

 
<?
$sweets = array("puff" => "A",
                 "square" => "C",
                 "cake" => "B",
                 "ricemeat" => "S");
print "<select name="sweet">";
foreach ($sweets as $option => $label) {
    print "<option value="" .$option .""";
    if ($option == $defaults["sweet"]) {
        print " selected="selected"";
    }
    print "> $label</option>\n";
}
print "</select>";
?>



Setting defaults in a multi-valued <select> menu

 
<?
$main_dishes = array("cuke" => "B",
                     "stomach" => "B",
                     "tripe" => "C",
                     "taro" => "S",
                     "giblets" => "E", 
                     "abalone" => "F");
print "<select name="main_dish[]" multiple="multiple">";
$selected_options = array();
foreach ($defaults["main_dish"] as $option) {
    $selected_options[$option] = true;
}
foreach ($main_dishes as $option => $label) {
    print "<option value="" . htmlentities($option) . """;
    if ($selected_options[$option]) {
        print " selected="selected"";
    }
    print ">" . htmlentities($label) . "</option>";
    print "\n";
}
print "</select>";
?>



Validating a drop-down menu with in_array()

 
<?php
$choices = array("Eggs","Toast","Coffee");
echo "<select name="food">\n";
foreach ($choices as $choice) {
   echo "<option>$choice</option>\n";
}
echo "</select>";
if (! in_array($_POST["food"], $choices)) {
    echo "You must select a valid choice.";
}
?>