从数组填充下拉框并使用php显示

时间:2013-10-11 01:17:48

标签: php html arrays

我正在尝试使用颜色数组的内容填充下拉框,然后在php文件中显示。还在php文件上显示单选按钮选择。我想不明白。我甚至没有开始在php文件中显示数组,因为我甚至不能填充下拉框。任何帮助将不胜感激。

我的HTML文件

    <!DOCTYPE html>
<?php
$Color = array("Red", "Blue", "Green", "Cyan", "Magenta", "Yellow");
$Food = array("Barbeque", "Chinese", "Japanese", "Thai", "Steak", "Vegan", "Mexican");
?>
<html lang="en">
<head><title>Lab 08</title></head> 
 <body> 
 <h2>Data Collection</h2><p> 
 <form method="get" action="test.php">
 <table> 
 <p>Select your favorite color.</p>
 <select name=Color> 


<?php

foreach($Color as $col)
    {
    echo "<option value=\"$col\">". $col ."</option>\n";    
}

?>
</select>
 <p>Select your favorite type of food.</p>
 <input type="radio" name="Food" value="Barbeque">Barbeque<br>
 <input type="radio" name="Food" value="Chinese">Chinese<br>
 <input type="radio" name="Food" value="Japanese">Japanese<br>
 <input type="radio" name="Food" value="Thai">Thai<br>
 <input type="radio" name="Food" value="Steak">Steak<br>
 <input type="radio" name="Food" value="Vegan">Vegan<br>
 <input type="radio" name="Food" value="Mexican">Mexican<br>
 <p></p>
 <tr><td>Enter your first name:</td><td><input name="First" type="text" /></td></tr> 
 <tr><td>Enter your last name:</td><td><input name="Last" type="text" /></td></tr> 

 <tr><td colspan="2" align="center"><input type="submit" value ="Submit"/></td></tr> 
 </table> 
 </form> 
 </body> 
 </html>

我的PHP文件

<!DOCTYPE html>
<?php
    $First = (isset($_GET['First']) ? $_GET['First'] : null);
    $Last = (isset($_GET['Last']) ? $_GET['Last'] : null);
    $Color = (isset($_GET['Color']) ? $_GET['Color'] : null);
    $Food = (isset($_GET['Food']) ? $_GET['Food'] : null);
?>
<html>
    <head>
    <title>Lab 08</title>
    </head>
    <body>
    <?php


if ($Color) {
echo "<p>$Color</p>";
    }
    else if (empty($Color)) {
    echo "<p>Please enter your favorite color.</p>";
    }
if ($Food)  {   
echo "<p>$Food</p>";
    }
    else if (empty($Food))  {
    echo "<p>Please enter your favorite food.</p>";
    }
if (!is_numeric($First) and (!empty($First)))   {
echo "<p>$First</p>";
    }
    else if (empty($First)) {
    echo "<p>Please enter first name.</p>";
    }
    else if (is_numeric($First))    {
    echo "<p>First name must be letters.</p>";
    }

if (!is_numeric($Last) and (!empty($Last)))     {
echo "<p>$Last</p>";
    }
    else if (empty($Last))  {
    echo "<p>Please enter last name.</p>";
    }

2 个答案:

答案 0 :(得分:0)

尝试:

//$row = null; 
while ($row = $col){

    echo "<option value=\"$row\">". $row ."</option>\n"; 
}   

答案 1 :(得分:0)

你在评论中写下

  

[...]第一个是HTML文件,第二个是php文件。 [...]

您无法在HTML文件中运行PHP代码。将第一个文件的文件扩展名从.html更改为.php以解决此问题。