我想在 php 中有一个简单的数组,在 html 中有一个组合框。
<?php
// Here is my array:
$cars=array("Volvo","BMW","Toyota");
?>
试试这个:
<?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>
试试这个
echo "<select name='cars'>";
foreach($cars as $car)
{
echo "<option value='".$car."'>".$car."</option>";
}
echo "</select>";
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>