0

I have a form with multiselect dropdown,on submit the values are passed through ajax by Seralize ,but only one value of multiselect dropdown is passed where i have selected more option on that dropdown.

var formdata= $("#form").serialize();
$.ajax(
{               
    url:base_url+"index.php/model/employee",  
    data: "employeedata="+employeeformdata,
    success: function(msg)
    { 
    //success message
    },                     
});

I have tried to get the values manually by mval=$('#multi').val(); and appended it to url,since i am having a dynamic form i cant specify the id of multiselect dropdown and one form can have many multiselect dropdowns,Is there any method to pass the values on serialize/serializeArray ?

sanjeeviraj
  • 135
  • 2
  • 10

2 Answers2

1

Not with serialize, and you might not have given values to your select options, but give this a try:

http://jsfiddle.net/94q7kcub/

dummycode

jQuery:

$('#clicker').click(function(){
var multiselect = $('#marketplaces').val();
alert(multiselect);
});

HTML:

<label for="marketplaces">Marketplaces:</label>
    <select class="select2" multiple id="marketplaces">
        <option value="DE">Germany</option>
        <option value="UK">United Kingdom</option>
        <option value="FR">France</option>
        <option value="ES">Spain</option>
        <option value="IT">Italy</option>
        <option value="US">United States</option>
</select>
<button id="clicker">click</button>
baao
  • 71,625
  • 17
  • 143
  • 203
1

The .serialize() method returns a string that looks like this:

selectNameA=valueA1&selectNameA=valueA2&selectNameB=valueB1

It is common to use the following when submitting a form via ajax with jQuery:

data: $("#form").serialize(),

Assuming you are using PHP on the server (since the question is tagged for PHP), you will want the value of the "name" attribute of the select element to end with "[]". (See this question.)

<select name="xxx[]" ...

That way, $_GET['xxx'] will be an array holding all the selected values.

Note: If you specify type: 'post' in your ajax call, you would use $_POST on the serveer, instead of $_GET.

Community
  • 1
  • 1
John S
  • 21,212
  • 8
  • 46
  • 56