View Full Version : Can't deselect an option in the second of two related lists.

05-29-2011, 04:18 PM
I have three dynamically related select lists. They feed each other with no issues. However, I have a problem. Let's say, you select an option from the first list and a new set of options appears in list two. You then select an option from list two and it shows as 'selected'. BUT...then.... you decide that you want to change the selection you made from the first list. Well, actually, that 's fine. The newly selected list-one option produces the new data in list-two. Here's the problem. The previously selected option in list two, remains, even though list two now has a set of new data. In fact, the selected option now has nothing to do with the new list of data. If I select an option from list two everything is once again up to date (however, my code produces an ugly double select blue band - one for the newly selected option and one for the previously selected option which is now blank). I would have thought 'going back to a previous option list and choosing again' would be a possible user action. But what about cancelling the previously selected option from list two ?
What I need is to be able to remove that list-two 'selected' when someone re-chooses an option from the first list. Here's my code which renders the second list:

<div id="Layer61">
<select name="second_phrase" select id = "second_phrase" class="phrasestyle1" size="16" MULTIPLE onChange="document.forms[0].submit();"><option value = "" selected>SECOND PHRASE LIST..............<option />

@$second_phrase = $_POST["second_phrase"];
@$first_phrase = $_POST["first_phrase"];
echo"<option value = '$second_phrase' selected>$second_phrase<option/>";
echo"<option value = '' selected><option/>";
$first_phrase = mysqli_real_escape_string($cxn, $first_phrase);
$query = ("SELECT phrase_id FROM phrases WHERE phrase_id = '$first_phrase'");
@$result = mysqli_query($cxn, $query) or die ("Could not execute query 2.");
@$row = mysqli_fetch_array(@$result);
@$phrase_id = $row['phrase_id'];
$query = ("SELECT second_phrase FROM next_phrases WHERE phrase_id = '$phrase_id'");
@$result = mysqli_query($cxn, $query) or die ("Could not execute query 3.");
while ($row = mysqli_fetch_array(@$result))
$second_phrase = $row['second_phrase'];

<option value=" echo $second_phrase"> echo $second_phrase</option>


I tried to go down the javascript route with this also. The following seems to be the correct code to do the job but from what I can read, you can't call it from within PHP because of parse sequencing. If you can call it from within PHP please let me know how it's done.

<script type="text/JavaScript">
function removeOptionSelected()
var elSel = document.getElementById("second_phrase");
var i;
for (i = elSel.length - 1; i>=0; i--) {
if (elSel.options[i].selected) {

An answer to this would be very much appreciated as I've been bumbling along with it for about two weeks.
All the best -

06-02-2011, 11:16 PM
If anyone is out there - I fixed it (well, I fell over it).
Javascript was the answer. This is the code:
function clearOptionSelected(id)
document.getElementById("second_phrase").options.selectedIndex = null;
You are possibly thinking that this is no big deal.
The big deal is that I was not aware that you could call two javascript functions with the one event. Hence this:
<select name="first_word" class="wordstyle" multiple size="16" onchange="clearOptionSelected();document.forms[0].submit();">
I'm absolutely certain that I read that you could not do this sort of thing.
Anyway, when you call it from the first list, it works. I'm hoping this might help someone.
All the best-