1

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?

Danil Speransky
  • 29,891
  • 5
  • 68
  • 79
user1525703
  • 37
  • 1
  • 8

1 Answers1

5

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/>";

}
?>
Sibu
  • 4,609
  • 2
  • 26
  • 38
  • the problem here is that i can only access the last selected index of the dropdown box. only one value is sent to my php. – user1525703 Sep 05 '12 at 13:40
  • @user1525703 i have checked it and its working correctly, see my edited answer i have included the html and php code – Sibu Sep 06 '12 at 04:11
  • I solved it, just get the double quotes in with the solid brackets. but thanks anyway.. data: {"Privilege[]": realvalues}, – user1525703 Sep 06 '12 at 07:19