0

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??

User2413
  • 605
  • 6
  • 22
  • 51

3 Answers3

0

Posting multipal value needs to be Array in selection box

Change selection box name geographicRegion[]

Below is the process to get value on client side when form submit it help

<form onsubmit="functionname($this)"></form>

On Client side

function functionname($form)
{
    var values = {};
     $.each($form.serializeArray(), function(i, field) {
         values[field.name] = field.value;
     });
    alert(values.field_name);  
}

In Ajax use async: false property to transfer data.

Hardik Raval
  • 1,948
  • 16
  • 29
0

we can save all the values in database by the following code:

var geolocations=$('#geographicregion :selected').map(function(){return $(this).val(); }).get();
        var totalloc="";
        for(var i=0;i<geolocations.length;i++)
        {
            totalloc+=geolocations[i]+",";

        }   

        alert("locations ========"+locations);

    var partners = JSON.stringify({
        "firstName": $('#fname').val(), 
        "lastName":$('#lname').val(),
        "geographicRegion":totalloc,


       });
User2413
  • 605
  • 6
  • 22
  • 51
0

just make it simple

var partners = JSON.stringify({
    "firstName": $('#fname').val(),
    "lastName":$('#lname').val(),
    "geographicRegion": $('#geographicregion').val()
});

EDITED CODE

           var geographicregion = $('#geographicregion').val().join();
           var partners = {
                "firstName": 'qwewqe',
                "lastName":'qwerewrwqe',
                "geographicRegion": geographicregion,
            };
rajesh kakawat
  • 10,826
  • 1
  • 21
  • 40