3

I was wondering if it is possible to pass an array to a php function using the jQuery AJAX function. I have the following as my javascript

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}

$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'field01': arr_data["field01"],
        'field02': arr_data["field02"],
        'field03': arr_data["field03"],
        'field04': arr_data["field04"]
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});

When I try to do the following though

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}

$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': arr_data
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});

I receive it in the PHP as "Array". How can I correctly send the object so that it is usable by the PHP function?

6
  • 2
    from the second ajax you can access the data like: $_GET['data']['field01'] Commented Nov 10, 2016 at 10:46
  • As you are sending the data as json , use json_decode in php function to convert the json to PHP array and then you can parse it Commented Nov 10, 2016 at 10:48
  • 1
    Just FYI, what you're sending is an object. I updated the question accordingly Commented Nov 10, 2016 at 10:50
  • Thanks for assistance @RoryMcCrossan Commented Nov 10, 2016 at 10:50
  • No wonder if you are sending object or object value as an array .. php will receive it in array in REQUEST [post/get] Commented Nov 10, 2016 at 10:53

3 Answers 3

9

Please try to pass the array in the format of json. Then use the get the json in your php and access the json array.

<script>

arr_data = {
    field01: "data 01",
    field02: "data 02",
    field03: "data 03",
    field04: "data 04"
}
var  myJsonString= JSON.stringify(arr_data);
$.ajax({
    url: "scripts/php/phpfunc.php",
    type: "GET",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': myJsonString
    },
    success: function(result) {
        // continue program
    },
    error: function(log) {
        // handle error
    }
});
</script>

this is your java script. and below is the php

$dataJson=json_decode($_GET['data']);

here you can get the json array and loop through it and do what ever you want.

Please have try at this. This is working in my case.

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

Comments

3

from the second ajax you can access the data based on the property names like: $_GET['data']['field01']

$_GET['data'] is the js object converted in php in a associative array

Comments

0

try this :

$.ajax({
    url: "scripts/php/phpfunc.php",
    method: "POST",
    dataType: "json",
    data: {
        'action': "exec_find",
        'data': arr_data.serialize()
    },

serialize()

http://api.jquery.com/serialize/

convert your array in string

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.