0

Was wondering if and how this is possible. I'm trying to pass multiple variables over to PHP with Jquery/Ajax. I can post multiple arrays if all of the variables are not arrays, but I can't if some of them are.

My Code:

<script>

var mutype;
var practiceArray = [];
var groupSizeArray = [];

function MUProvidersContinue() 
{

    variableString ='mutype='+mutype+
                    '&practiceArray='+practiceString+
                    '&groupSizeArray='+groupSizeString;

    jQuery.ajax({type: "POST", url: "musetup_auth.php", data: variableString,});

};

I know i need to use JSON.stringify somewhere in there, but not sure how or where.

Thanks in advance.

1
  • have you tried datatype: json? Commented Jun 13, 2013 at 20:08

3 Answers 3

2

Better is to send data as object:

var data = {
    mutype: mutype,
    practiceArray: practiceString,
    groupSizeArray: groupSizeString
};

jQuery.ajax({type: "POST", url: "musetup_auth.php", data: data},function(json){});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks - this was the best way. Makes sense
2

Normally when doing jQuery.ajax you want to pass an object, and it will handle stringifying for you IIRC.

So in your case:

jQuery.ajax({
    type: "POST",
    url: "musetup_auth.php",
    data: {
        mutype: mutype,
        practiceArray: practiceArray,
        groupSizeArray: groupSizeArray
    }
});

Comments

0
var mutype;
var practiceArray = [];
var groupSizeArray = [];
var sendArray = [];

var sendArray['mutype']=mutype;
var sendArray['practiceString']=practiceArray;
var sendArray['groupSizeString']=groupSizeArray;


function MUProvidersContinue() 
{

  jQuery.ajax({
               type: "POST",
               url: "musetup_auth.php", 
               data: sendArray
              });

};

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.