Reputation: 169
I have a multi-select box which allows me to select multiple values and then add them to a second select box when I click on an arrow. Now this part works fine and I can select multiple values. However when I pass the values of the second select box to variable, to a PHP page the variable only shows the value of the last item in the list and not all of them.
Javascript Code
<script type="text/javascript">
$().ready(function() {
$('#add').click(function() {
return !$('#select1 option:selected').remove().appendTo('#select2');
});
$('#remove').click(function() {
return !$('#select2 option:selected').remove().appendTo('#select1');
});
});
</script>
HTML CODE
<form action="insert_email_visitor_list1.php" method="get" name="form1" target="_self" id="form1">
<select multiple id="select1" class="mulitiselect" name="select1">
<option value="1">Date/Time</option>
<option value="2">Company</option>
<option value="3">Location</option>
<option value="4">Nof Pages / Visit</option>
<option value="5">Traffic Source</option>
<option value="6">Search Term</option>
<option value="7">Report</option>
<option value="8">Classification</option>
<option value="9">Owner</option>
<option value="10">Phone</option>
<option value="11">Town</option>
<option value="12">City</option>
<option value="12">Country</option>
<option value="12">Hostname</option>
</select>
<a href="#" id="add"><img src="images/add-arrow.png" alt="" /></a>
selected Columns
<a href="#" id="remove"><img src="images/remove-arrow.png" alt="" /></a>
<select multiple id="select2" name="select2" class="mulitiselect"></select>
<div class="bottom-img"><img src="images/popup-bottomimg.png" alt="" /></div>
<button>
<img src="images/info-icon.png" alt="" />
</button>
<input type="submit" name="send" id="send" value="Submit" />
</form>
PHP CODE
$select2 = $_GET['select2'];
echo "$select2";
Basically I am just after some advice as to if i am doing this the correct way?
Thanks
Upvotes: 3
Views: 6891
Reputation: 360732
The field you pass to PHP must be named with a []
, e.g. select2[]
. Without that, PHP will treat the passed in values as a single value, and only the LAST value passed in would ever bee put into $_GET.
Using []
on the name tells PHP to treat it as a multi-valued field and it will create an array in $_GET, e.g.
$_GET['select2'][0] => 'first option selected';
$_GET['select2'][1] => 'second option selected';
etc...
Remember that the multiple
parameter is a purely client-side thing, and PHP has no way of knowing that it was a multi-select. It willy simply see the equivalent of
select2=foo&select2=bar&select2=baz
arrive in the POST/GET, and select the last value passed in.
Upvotes: 7