user1525703
user1525703

Reputation: 37

passing to ajax an array from multiple selected dropdown

this is my Jquery:

$('#Save').click(function () {
  var realvalues = new Array(); //storing the selected values inside an array

  $('#Privilege :selected').each(function (i, selected) {
    realvalues[i] = $(selected).val();
  });

  $.ajax({
    type: "POST",
    traditional: true,
    url: "http://localhost:8081/crownregency/UpdateOrCreateOrDeleteUser.php",
    data: {
      Privilege: realvalues,
      ID: '1'
    },
    success: function (data) {
      alert(data, 'Status');
      location.reload();
    }
  });
});

this is my php.

I have read quiet a lot about serializing but doesnt seem to work, what i am trying to achieve is sending the selected items of a dropdown into an array and sending it to a php through ajax. but sending the array to the php doesnt seem to work. help anyone?

Upvotes: 1

Views: 7708

Answers (1)

Sibu
Sibu

Reputation: 4617

Your code is okay just remove the traditional: true and your code seems to work

$('#Save').click(function(){

    var realvalues = new Array();//storing the selected values inside an array
    $('#Privilege :selected').each(function(i, selected) {
        realvalues[i] = $(selected).val();
    });

    $.ajax({
        type: "POST",
        url: "http://localhost:8081/crownregency/UpdateOrCreateOrDeleteUser.php",
        data: {Privilege: realvalues, ID: '1'},
        success:function(data){
                $("#subscrres").html(data)
            }
    });
});

HTML

<form method="post">
<select id="Privilege" multiple="multiple">
<option value="yahoo">yahoo</option>
<option value="chrome">chrome</option>
<option value="mozilla">mozilla</option>
</select>
<input type="button" id="Save"/>

</form>

UpdateOrCreateOrDeleteUser.php

<?php
if(isset($_POST['Privilege'])){
$myvar  =$_POST['Privilege'];
foreach($_POST['Privilege'] as $one)
echo $one."<br/>";

}
?>

Upvotes: 5

Related Questions