1

I want to pass arrays and other variables from jquery code to php using post request but I don't know how to pass arrays and variables at the same time.

I tried this code:

var new_location_name = "";
var new_location_id = "";
var old_location_name = [];
var selected_name = [];
var selected_id = [];
var user = '<?php echo $current_user ;?>';

    $(document).ready(
           $("#transfer").click(function(){
                 $.ajax({
                    type: "POST",
                    url: "update.php",
                    data: "username="+user+"&new_location_name="+new_location_name+"&new_location_id"+new_location_id+"&"+{ old_location_name : old_location_name }+"&"+{ selected_name : selected_name }+"&"+{ selected_id : selected_id },
                });
                 return false;
            });
    });

5 Answers 5

2

Create an object, serialize it and send it:

var obj = {
   key1: 'value1',
   key2: ['one','two','three'],
   key3: {
      key3_0: 'value3_0'
   }
}
var jsonData = JSON.stringify(obj);

$.ajax({
     method: "POST",
     url: "update.php",
     data: jsonData
});
Sign up to request clarification or add additional context in comments.

Comments

1

JQuery Data:

data: 
    {
        username: user, 
        new_location_name: new_location_name, 
        new_location_id: new_location_id, 
        old_location_name: old_location_name, 
        selected_name : selected_name, 
        selected_id : selected_id 
    }

In your PHP Script

<? $oldLocationName = $_REQUEST['old_location_name']; ?>

and so on.

Comments

0

You can pass multiple parameters as arrays or strings the same. The left hand side of the data is the value that the API is expecting as the parameter name.

var new_location_name = "";
var new_location_id = "";
var old_location_name = [];
var selected_name = [];
var selected_id = [];
var user = '<?php echo $current_user ;?>';

    $(document).ready(
           $("#transfer").click(function(){
                 $.ajax({
                    type: "POST",
                    url: "update.php",
                    data: {username: user, 
                           new_location_name: new_location_name,
                          old_location_name: old_location_name
                      }
                });
                 return false;
            });
    });

You can read more in this question: Pass Multiple Parameters to jQuery ajax call

2 Comments

thanks for the quick response, if I sent arrays and variables like this what would I need to do on the other side at the php file to get the value of each array and each variable?
They would just be parameters on the php function and then you can access them as JSON.
0

Try this snippet

var array = [];
$.ajax({ url: 'update.php',
    data: {'array' : array},
    type: 'post',
    dataType:'json',
    success: function(output) {
        alert(output);
    },
    error: function(request, status, error) {
       alert("Error");
    }
});

Comments

-1

First time Check file, this script must be in .php file, not .js file;

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.