-1

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>
George
  • 31
  • 4
  • 10
  • You need to wrap that in a `
    `, and if you are using the "get" method, just then read `$_GET['cars']`, and if it is non-null, a selection has been made. You'll need a submit button too, of course (if you want something immediately dynamic, then you'll need JavaScript).
    – halfer Aug 21 '13 at 11:32
  • you want to alert "Your Favourite Car is Volksvagen!" upon selection using javascript ? – Prince Singh Aug 21 '13 at 11:35
  • i just want to display it on the website, its a task i was given to do with php so i cant use JavaScript – George Aug 21 '13 at 11:37

3 Answers3

1

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; 
?>
chirag ode
  • 950
  • 7
  • 15
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>
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>

Try This

Eugine Joseph
  • 1,552
  • 3
  • 18
  • 40