0

I try to implement the following code

 var flag = new Array();
 var name = $("#myselectedset").val();      
$.ajax({
    type:         'post',
    cache:         false,
    url:          'moveto.php',
    data:     {'myselectset' : name,
              'my_flag' : flag
              },
    success: function(msg){
                     $("#statusafter").ajaxComplete(function(){$(this).fadeIn("slow").html(msg)});
                      }     
          });       

You can see that the name is a single string and the flag is an arry, am I using the right format for passing them throw ajax call, anyone could help me, thanks

2 Answers 2

1

It is impossible to pass arrays in a POST request. Only strings.

You will either need to stringify your array, or consider posting as JSON instead.

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

3 Comments

@Truth While your second sentence is good advice your first sentence is misleading if not outright false. Most web development platforms, including PHP, will interpret POST-data in the form arr[]=foo&arr[]=bar&arr[]=baz as an array value named arr with length 3.
so can I do this: data: 'myselectset'+name+'&my_flag[]='+flag?
@Jordan what I mean is that you can't pass arrays, JavaScript arrays, in a post request, you can send what you wrote in the form of a string, and PHP will interpret it as an array. But not the other way around.
0

You should be able to do something quite simple, like replace your "data" property with:

data : JSON.stringify( { myselectset : name, my_flag : flag } )

That will convert the data into a JSON string that you can turn into PHP on the other side, using json_decode($_POST["my_flag"]);

Very important note:

For JSON.stringify to work, there can't be any functions in the array - not even functions that are object methods.

Also, because this is a quick example, make sure that you're testing for null data and all of the rest of the best-practices.

Comments

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.