we're using the event handler onChange(); so theoretically it means "on the Change of this select box" do something. And since we only care about the results after they've hit the SUBMIT button then we will only need to store what they have in the select box at the moment they hit SUBMIT.
In other words, we don't care what they do and what hobbiesList contains if they were just messing around with the form. The real value is after they hit submit.
The trick then is resetting the hobbiesList when the page loads or right a wrapper for it and make it a helper function. The only time you won't reset is if there is a previous value and that previous value is part of what is currently selected
so the trick is to check for occurrences that occur in the array of results
lets run through the algorithm together. for example: (i'm using pseudo-code for purposes of this discussion)
<option value="1">1
<option value="2">2
<option value="3">3
if I CTRL-selected 1, 2, 3 then hobbiesList should have 1, 2, 3 right?
// set up array, array should contain available options. If its possible set up an associative array such that array["option"] = value; sort of like a hash.
var array = array();
is 1 selected? yes. is it in array? no. add to array.
is 2 selected? yes. is it in array? no. add to array.
is 3 selected? yes. is it in array? no. add to array.
return array
now let say i'm the indecisive type and I change my mind all the time. so this time I hit 1, thus clearing 2, 3 then I go into my function and check
is 1 selected? yes. is it in array? yes. do nothing.
is 2 selected? no. is it in array? yes. remove from array.
is 3 selected? no. is it in array? yes. remove from array.
return array
so what happens if i change my mind again, maybe i'm really just bored, and I decided that I like 1 AND also 3 so I call my function again and do the following:
is 1 selected? yes. is it in array? yes. do nothing
is 2 selected? no. is it in array? no. do nothing
is 3 selected? yes. is it in array? no. add to array
return array
so now the trick is storing the results in an array and once the user hits submit
the array contents is dumped into a hidden field and the hidden field is then passed into your PHP function and you can parse it using explode();
that took a lot of thinking so I didn't have time to write the code. i hope that helps.
good luck my friend.
jm _