PHP下拉框

时间:2013-08-21 11:31:27

标签: php dropdownbox

我有一个工作下拉框,但我不知道怎么做,以便如果他们选择“volksvagen”它会显示

“你最喜欢的车是Volksvagen!”

每个选项的

等等,下面是下拉框的代码。

<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');

echo' <select name="cars">';

foreach($array1 as $cars){
        echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
</div>

3 个答案:

答案 0 :(得分:1)

javascript没用过..它吧!! !!

<form method="post">
<div id="dropdown">
<?php
if(isset($_POST['cars']))
{
$mycar=$_POST['cars'];
}
else
{
$mycar='';
} 
$array1 = array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" onchange="this.form.submit()">';
foreach($array1 as $cars){ ?>
<option value="<?php echo $cars; ?>" <?php if($mycar==$cars) echo "selected='selected'"; ?> ><?php echo $cars; ?></option>
<?php
}
echo'</select>
</div></form>';
echo 'your favourite car is : '; echo $mycar; 
?>

答案 1 :(得分:0)

<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');

echo' <select name="cars" onchange="display_message(this.value);">';

foreach($array1 as $cars){
        echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<span id="message"></span>
</div>
<script>
function display_message($selected_value)
{
document.getElementById('message').innerHTML = 'Your Favourite Car is'+$selected_value+'!';
}
</script>

答案 2 :(得分:0)

<body onload="showCar()">
<div id="dropdown">
<?php
$array1 =
array('Volkswagen' , 'Renault' , 'Land Rover');
echo' <select name="cars" id="cars" onchange="showCar()">';
foreach($array1 as $cars){
        echo'<option value="'.$cars.'">'.$cars.'</option>';
}
echo'</select>';
?>
<div id="demo"> </div>
<script> 
function showCar() {
    var car = document.getElementById('cars').value; 
    document.getElementById("demo").innerHTML="Your Favourite Car is "+car+"!";
}
</script>
</div>  
</body>

试试这个