Reputation: 184
I have a dropdown box which has true or false for options. If true is selected, I want a selected picture to be displayed. If false is selected, I want a different picture to be displayed
Here is my code so far:
<form>
<select name="Picture">
<option value= <?php $Test = True ?>"True">True</option>
<option value=<?php $Test = False ?>"False">False</option>
<?php
if ($Test == "True")
echo "<img src = '../Images/Worked.jpg'>";
else
echo "<img src = '../Images/DidntWork.jpg'>";
?>
</select>
</form>
All I seem to get is the drop down menu but no images. Also I'm not sure that the true/false selector is correct.
Upvotes: 0
Views: 2166
Reputation: 227270
There are a few things wrong here.
First, <?php $Test = True ?>
. This sets $Test
. You need to do ==
to compare (like you do later on).
Second, you cannot have <img>
tags as children of a <select>
. You'll need to place them outside of the <select>
.
Third, you use the selected
attribute to mark which <option>
should be selected.
<form>
<select name="Picture">
<option <?=$Test == "True" ? 'selected' : ''?> value="True">True</option>
<option <?=$Test == "False" ? 'selected' : ''?> value="False">False</option>
</select>
<?php
if ($Test == "True"){
echo "<img src = \"../Images/Worked.jpg\">";
}
else {
echo "<img src = \"../Images/DidntWork.jpg\">";
}
?>
</form>
Note: This will only display the image when the page is loaded. If you want it to change when the <select>
is changed, you'll need to use JavaScript (or reload the page after you save the value).
Upvotes: 3