开发者

making a sticky form in php

i am trying to have a form i'm making have the form values selected if they exist in $_POST but i can't figure out the logic to get that in place because placing "selected" in with foreach will cause each one to be selected.


   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org   /TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>

    <title>Lab 2</title>
    <style type="text/css" >

    body {background-color:#<?php 
        if(!isset($_POST['background']))
            print 'fefa8f';
        else print $_POST['background'];?>;width:1000px auto;}

    table {background-color:#6677CC;padding:30px;border:5px dashed black;text-align:center;margin:0 auto;font-family:tahoma;}
    </style>
</head>

<body>

<?php 
ini_set('display_errors', 1);
print '<fieldset style="background-color:#ccffcc;border:2px dashed black;text-align:center;font-family:tahoma;width:400px"><legend style="border: 1px solid;padding:2px 6px;background-color:#ffcc99">Contents of $_POST</legend>';
var_dump($_POST);
print "</fieldset>";
?>


<h1 style="font-family:tahoma;text-align:center;">Form 1</h1>

<table width="50%">

<tr style="text-transform:capitalize;">

<td>


<?php


$colors = array(
 'lightcoral' => 'F08080', 'rosybrown' => 'BC8F8F', 'indianred' => 'CD5C5C',
 'red' => 'FF0000', 'firebrick' => 'B22222', 'phpmyadmin-orange' => 'ffcc99', 'phpmyadmin-green' => 'ccffcc', 'brown' => 'A52A2A',
 'darkred' => '8B0000', 'maroon' => '800000', 'mistyrose' => 'FFE4E1',
 'salmon' => 'FA8072', 'tomato' => 'FF6347', 'darksalmon' => 'E9967A',
 'coral' => 'FF7F50', 'orangered' => 'FF4500', 'lightsalmon' => 'FFA07A',
 'sienna' => 'A0522D', 'seashell' => 'FFF5EE', 'chocolate' => 'D2691E',
 'saddlebrown' => '8B4513', 'sandybrown' => 'F4A460', 'peachpuff' => 'FFDAB9',
 'peru' => 'CD853F', 'linen' => 'FAF0E6', 'bisque' => 'FFE4C4',
 'darkorange' => 'FF8C00', 'burlywood' => 'DEB887', 'antiquewhite' => 'FAEBD7',
 'darkgoldenrod' => 'B8860B', 'goldenrod' => 'DAA520', 'cornsilk' => 'FFF8DC',
 'gold' => 'FFD700', 'lemonchiffon' => 'FFFACD', 'khaki' => 'F0E68C',
 'palegoldenrod' => 'EEE8AA', 'darkkhaki' => 'BDB76B', 'ivory' => 'FFFFF0',
 'lightyellow' => 'FFFFE0', 'beige' => 'F5F5DC', 'lightgoldenrodyellow' => 'FAFAD2',
 'yellow' => 'FFFF00', 'olive' => '808000', 'olivedrab' => '6B8E23',
 'yellowgreen' => '9ACD32', 'chartreuse' => '7FFF00', 'darkseagreen' => '8FBC8B',
 'honeydew' => 'F0FFF0', 'lightgreen' => '90EE90',
 'lime' => 'iumspringgreen', 'mediumaquamarine' => '66CDAA', 'aquamarine' => '7FFFD4',
 'turquoise' => '40E0D0', 'lightseagreen' => '20B2AA', 'mediumturquoise' => '48D1CC',
 'azure' => 'F0FFFF', 'lightcyan' => 'E0FFFF', 'paleturquoise' => 'AFEEEE',
 'aqua' => '00FFFF', 'cyan' => '00FFFF', 'darkcyan' => '008B8B',
 'teal' => '008080', 'darkslategray' => '2F4F4F', 'darkturquoise' => '00CED1',
 'cadetblue' => '5F9EA0', 'powderblue' => 'B0E0E6', 'lightblue' => 'ADD8E6',
 'deepskyblue' => '00BFFF', 'skyblue' => '87CEEB', 'lightskyblue' => '87CEFA',
 'steelblue' => '4682B4', 'aliceblue' => 'F0F8FF', 'dodgerblue' => '1E90FF',
 'lightslategray' => '778899', 'midnightblue' => '191970', 'navy' => '000080',
 'slateblue' => '6A5ACD', 'darkslateblue' => '483D8B', 'mediumslateblue' => 开发者_StackOverflow'7B68EE',
 'mediumpurple' => '9370DB', 'blueviolet' => '8A2BE2', 'indigo' => '4B0082',
 'darkorchid' => '9932CC', 'darkviolet' => '9400D3', 'mediumorchid' => 'BA55D3',
 'thistle' => 'D8BFD8', 'plum' => 'DDA0DD', 'violet' => 'EE82EE',
 'fuchsia' => 'FF00FF', 'magenta' => 'FF00FF', 'darkmagenta' => '8B008B',
 'purple' => '800080', 'orchid' => 'DA70D6', 'mediumvioletred' => 'C71585',
 'deeppink' => 'FF1493', 'hotpink' => 'FF69B4', 'lavenderblush' => 'FFF0F5',
 'palevioletred' => 'DB7093', 'crimson' => 'DC143C', 'gray' => '808080',
 'dimgray' => '696969', 'black' => '000000'
);

//sort array by key in alphabetical order 
ksort($colors);

print "<form action='' method=\"post\">";


//print key/value pairs from the colors array, then print those names
//as radio buttons
foreach($colors as $k => $v){
        print "<input type=\"radio\" name=\"background\" value=\"$v\" title=\"$k\" id=\"$k\" />$k\n";
        }



//submit button
print "<br /><br /><input type=\"submit\" name=\"Submit\" value=\"Submit\" />";

print "</form></td></tr></table>";




?>

<br /><br /><br />
<h1 style="font-family:tahoma;text-align:center;">Form 2</h1>

<table>
<tr><td>



<form action='' method="post">

<select style="font-family:tahoma; text-align:center;text-transform:capitalize;" name="background">

<?php
//printing the array as a drop down with each 
//selection having its own background color
foreach($colors as $k => $v){
        print "<option style='background-color:#$v' value='$v'>$k</option>\n";
        }

print "</select>";

//submit button
print "<br /><br /><input type=\"submit\" name=\"Submit\" value=\"Submit\" />";

print "</form>";
?>
</td></tr>
</table>
<br /><br /><br /><br />

<h1 style="font-family:tahoma;text-align:center;">Form 3</h1>
<table width="50%">
<tr>
<td>
<form action='' method="post">
<?php


//print selection as a color box 50px x 50px
foreach($colors as $k => $v){
        echo "<div style=\"height:50px;width:50px;float:left;margin:auto;background-color:#$v\">
        <input type=\"checkbox\" name=\"background\" value=\"$v\" title=\"$k\" /></div>";
}

?>


<input type="submit" name="submit" value="Submit" />

</form>
</td>
</tr>
</table>



</body>
</html>


Generally, you would do something like

foreach($colors as $k => $v)
    {
        if(in_array($v, $_POST))
        {
            print "<input type=\"radio\" name=\"background\" value=\"$v\" title=\"$k\" id=\"$k\" selected=\"selected\"/>$k\n";
        }
        else
        {
             print "<input type=\"radio\" name=\"background\" value=\"$v\" title=\"$k\" id=\"$k\" />$k\n";
        }
    }

This checks to see if that value exists in the $_POST array, and if it does, it will flag it as selected. If not, it will render as normal. The way in_array() will be set up is dependent on whether you want to search for the color key ($k) or color value ($v).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜