Pregunta

I have a working drop down box but im not sure how to make it so that if they chose "volksvagen" it would display

"Your Favourite Car is Volksvagen!"

and so on for each of the options, here is the code for the drop down box.

<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>
¿Fue útil?

Solución

javascript not used..try it..!!

<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; 
?>

Otros consejos

<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>
<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>

Try This

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top