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;
            });
    });
Kareem Essam Gaber
  • 643
  • 1
  • 6
  • 16

5 Answers5

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
});
kapantzak
  • 11,610
  • 4
  • 39
  • 61
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.

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

Avitus
  • 15,640
  • 6
  • 43
  • 53
  • 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? – Kareem Essam Gaber May 24 '17 at 14:01
  • They would just be parameters on the php function and then you can access them as JSON. – Avitus May 24 '17 at 14:03
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");
    }
});
Louay Hamada
  • 781
  • 1
  • 14
  • 22
-1

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