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?

4
  • Have you checked this: stackoverflow.com/questions/4402036/… Commented Aug 31, 2012 at 3:16
  • could you paste your html code Commented Aug 31, 2012 at 4:05
  • yes, it didnt solve my problem Commented Aug 31, 2012 at 11:18
  • @user1525703 please chek my answer Commented Sep 3, 2012 at 6:58

1 Answer 1

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

}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

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 i have checked it and its working correctly, see my edited answer i have included the html and php code
I solved it, just get the double quotes in with the solid brackets. but thanks anyway.. data: {"Privilege[]": realvalues},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.