6

I have two MultiSelect lists (AllProductList and SelectedProductList), AllProductList contains all products for a particular category and I add/clone options from the AllProductList to the SelectedProductList using JQuery.

I obviously only wish to post the values in the SelectedProductList and irrespective of whether they are selected or not.

I have wrapped the form tags around the SelectedProductList only and now need some way to post all option values in it, irrespective whether selected or not.

2
  • 4
    Select them all programmatically before submission.
    – Leonid
    Jan 25, 2012 at 5:13
  • Yeah, that sounds a lot more plausible and hassle free
    – LaserBeak
    Jan 25, 2012 at 5:22

2 Answers 2

2

you could write some javascript that fills a hidden form element with all the values from the select, something like below, and on the serverside just use explode(",",$_POST["allValues"]) to get all options

<script>
var hiddenValues = "";
$(document).ready(function(){
   $("#mySelect option").each(function(){
       hiddenValues = $(this).val() + ",";
   })//end each

   $("#myForm").append("<input type='hidden' name='allValues' value='"+hiddenValues+"'>")
})
</script>

obviously, the above has a dependency on jQuery and your form has a id of myForm and that your multiselect has a id of mySelect :)

EDIT :
NOTE1 : that this saves only the values of the options, and not the labels from the select (a similiar method can be employed to save those as well). just keep this in mind

NOTE2 : beware if the values contain any commas, as this will invalidate your input (if not escaped in a way, or if not using some other delimiter)

2
  • I changed #mySelect options to #mySelect option and it worked for me. thanks :)
    – Parag
    Apr 20, 2013 at 3:55
  • you're right, that should be "option", i'll add it to my answer
    – Bogdan
    Apr 20, 2013 at 19:18
1

I can't comment, so I guess i have to put an answer???

Bogdan's answer needs +=

hiddenValues += $(this).val() + ",";

otherwise you only get 1...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.