2

我想在 php 中有一个简单的数组,在 html 中有一个组合框。

<?php
// Here is my array:
$cars=array("Volvo","BMW","Toyota");
?>
4

3 回答 3

1

试试这个:

<?php
$cars=array("Volvo","BMW","Toyota");

if ($_SERVER['REQUEST_METHOD']==="POST") {
    if (isset($_POST['car'])) {
        if (in_array($_POST['car'],$cars)) {
            echo "You selected ".$_POST['car']."!";
            exit;
        }
    }
}

?>
<DOCTYPE html>
<html>
    <form method="post" action="">
        <select name="car">
        <?php
        foreach ($cars as $car) {
            echo '<option value="'.$car.'">'.$car.'</option>';
        }
        ?>
        </select>
        <input type="submit" value="Select">
    </form>
</html>
于 2013-05-28T05:44:45.487 回答
0

试试这个

echo "<select name='cars'>";
foreach($cars as $car)
{
   echo "<option value='".$car."'>".$car."</option>";
}
echo "</select>";
于 2013-05-28T05:42:30.460 回答
0

php页面中的代码:

<?php
$cars=array("Volvo","BMW","Toyota");
$smarty->assign('cars', $cars);
?>

HTML页面(模板页面)的Smarty代码:

<select name="car">
  {foreach from=$cars item=car}
    <option value="{$car}">{$car}</option>
  {/foreach}
</select>
于 2013-05-28T06:07:59.373 回答