PHP/Form/Form Select

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

A day choice <select> menu

   <source lang="html4strict">

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

 </source>
   
  


An HTML Form Including a SELECT Element

   <source lang="html4strict">

<html> <head> <title>An HTML form including a SELECT element</title> </head> <body> <form action="formSelectData.php" method="POST">

 <input type="text" name="user">
 
<textarea name="address" rows="5" cols="40"></textarea>
<select name="products[]" multiple> <option>option1 <option>option2 <option>option3 <option>option4 </select>
<input type="submit" value="OK">

</form> </body> </html>


      </source>
   
  


An HTML Form with a select Element

   <source lang="html4strict">

<html> <head> <title>An HTML Form with a "select" Element</title> </head> <body>

<form action="index.php" method="post">

<input type="text" name="user" />

<textarea name="address" rows="5" cols="40"> </textarea>

<select name="products[]" multiple="multiple"> <option>A</option> <option>B</option> <option>C</option> <option>D</option> </select>

<input type="submit" value="hit it!" />

</form>

</body> </html> //Reading Input from the Form <html> <head> <title>Reading Input from the Form</title> </head> <body>

<?php
 print "Welcome " . $_POST ["user"] . "
"; print "Your address is:
" . $_POST ["address"] . "
\n"; if (is_array ( $_POST ["products"] )) {
print "

Your product choices are:

"; print "
    "; foreach ( $_POST ["products"] as $value ) { print "
  • $value
  • \n";
       }
    
    print "
";
 }
 ?>

</body> </html>

 </source>
   
  


A <select> menu with different choices and values

   <source lang="html4strict">

<? $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>
<input type="submit" value="Order"> <input type="hidden" name="_submit_check" value="1"> </form> _HTML_; } ?>

 </source>
   
  


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

   <source lang="html4strict">

<html> <body>

   <?php
     if ($_POST["submitted"] == "yes"){
       echo $_POST["month"] . "/" . $_POST["day"] . "/" . $_POST["year"] . " - " . $_POST["hour"] . ":" . $_POST["minute"] . ":" . $_POST["second"];
       ?>
<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" />
         Select a Date and Time: 
<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>
<input type="submit" value="Submit" style="margin-top: 10px;" /> </form> <?php }  ?> </div>

</body> </html>

 </source>
   
  


Creating Form Elements with Multiple Options

   <source lang="html4strict">

<html> <body> <?php if ($_POST ["submitted"] == "yes") {

 if (count ( $_POST ["fruit"] ) != 0) {
   echo "Your Selections:
"; } else { echo "You have not made any selections.

"; } for($i = 0; $i < count ( $_POST ["fruit"] ); $i ++) { echo $_POST ["fruit"] [$i] . "
"; }  ?><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):
<select name="fruit[]" multiple="multiple">

 <option value="Bananas">Bananas</option>
 <option value="Apples">Apples</option>
 <option value="Oranges">Oranges</option>

</select>
<input type="submit" value="Submit" /></form> <?php } ?> </body> </html>

 </source>
   
  


Displaying a <select> menu

   <source lang="html4strict">

<? $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>
<input type="submit" value="Order"> <input type="hidden" name="_submit_check" value="1"> </form> _HTML_; } ?>

 </source>
   
  


Form select input

   <source lang="html4strict">
      
      

<HTML> <HEAD> <TITLE>Candy preference form</TITLE> </HEAD>

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



      </source>
   
  


Generating a dynamic pull-down menu

   <source lang="html4strict">

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

 </source>
   
  


One choice for each day from 1 to 31

   <source lang="html4strict">

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

 </source>
   
  


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

   <source lang="html4strict">

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

 </source>
   
  


Setting a default value in a <select> menu

   <source lang="html4strict">

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

 </source>
   
  


Setting defaults in a multi-valued <select> menu

   <source lang="html4strict">

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

 </source>
   
  


Validating a drop-down menu with in_array()

   <source lang="html4strict">

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

} ?>

 </source>