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?
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?
Share Improve this question edited Nov 10, 2016 at 10:49 Rory McCrossan 338k41 gold badges320 silver badges351 bronze badges asked Nov 10, 2016 at 10:40 TheLovelySausageTheLovelySausage 4,10417 gold badges65 silver badges115 bronze badges 6- 2 from the second ajax you can access the data like: $_GET['data']['field01'] – madalinivascu 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 – Webdev Commented Nov 10, 2016 at 10:48
- 1 Just FYI, what you're sending is an object. I updated the question accordingly – Rory McCrossan Commented Nov 10, 2016 at 10:50
- Thanks for assistance @RoryMcCrossan – TheLovelySausage 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] – Hitesh Jangid Commented Nov 10, 2016 at 10:53
3 Answers
Reset to default 9Please 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.
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
try this :
$.ajax({
url: "scripts/php/phpfunc.php",
method: "POST",
dataType: "json",
data: {
'action': "exec_find",
'data': arr_data.serialize()
},
serialize()
http://api.jquery./serialize/
convert your array in string