Jquery:
<script type="text/javascript">
function becomepartner()
{
var locations = [];
$('#geographicregion :selected').each(function(i, selected){
locations[i] = $(selected).val();
});
alert("locations ========"+locations);
var partners = JSON.stringify({
"firstName": $('#fname').val(),
"lastName":$('#lname').val(),
"geographicRegion":locations,
});
$.ajax({
type: "POST",
contentType: 'application/json',
url: baseurl+"becomepartner/add",
data: partners,
dataType:"text",
success:successmethod,
error: function(data,status) {
//alert("Error "+status);
}
});
}
HTML :
<form class="form-horizontal" id="becomepartnerfrm" style="padding:20px;" onsubmit="becomepartner(); return false;">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="fname">First Name</label>
<div class="col-md-5">
<div class="required-field-block">
<input id="fname" name="firstName" type="text" placeholder="First Name" pattern="[A-Za-z ]{3,20}" oninvalid="setCustomValidity('First Name should be in range of 3 to 20 characters(only alphabets are allowed')" onchange="try{setCustomValidity('')}catch(e){}" class="form-control input-md" required="required">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="lname">Last Name</label>
<div class="col-md-5">
<div class="required-field-block">
<input id="lname" name="lastName" type="text" placeholder="Last Name" pattern="[A-Za-z ]{3,20}" oninvalid="setCustomValidity('Last Name should be in range of 3 to 20 characters(only alphabets are allowed')" onchange="try{setCustomValidity('')}catch(e){}" class="form-control input-md" required="required">
<div class="required-icon">
<div class="text">*</div>
</div>
</div>
</div>
</div>
<!-- Select Multiple -->
<div class="form-group">
<label class="col-md-4 control-label" for="geolocation">Business Locations/Regions</label>
<div class="col-md-5">
<select id="geographicregion" name="geographicRegion" class="form-control" multiple="multiple">
<option value="APAC">APAC</option>
<option value="EMEA">EMEA</option>
<option value="LATAM">LATAM</option>
<option value="Middle East">Middle East</option>
<option value="NA">NA</option>
<option value="Public Sector">Public Sector</option>
<option value="All">All</option>
</select>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for=""></label>
<div class="col-md-4">
<button type="submit" class="btn btn-success">Apply Now</button>
</div>
</div>
</form>
In the above code i have multiple select box..i am selecing three values from it and getting it inside function..but only the last value is saving inside database instead of all the three values selected in the multiple select box..please throw some light on it??