I'm making a jQuery .post() with the following arrays:
'cleanedLinkStructureArray[]': cleanedLinkStructureArray,
'cleanedPermaLinkArray[]': cleanedPermaLinkArray
The data inside these arrays: cleanedPermaLinkArray looks like this: ["2012","10","30","hello-world"] and cleanedLinkStructureArray like this: ["year","monthnum","day","postname"]
Javascript code:
var ajaxPost = $.post(
enableAJAX.ajaxurl,
{ action: 'ajaxRequest',
'ajaxRequestNonce' : enableAJAX.ajaxRequestNonce,
'cleanedLinkStructureArray[]': cleanedLinkStructureArray,
'cleanedPermaLinkArray[]': cleanedPermaLinkArray },
'json'
);
ajaxPost.done(function(responseText) {
alert(responseText);
console.log(responseText);
});
ajaxPost.fail(function() {
alert("Oops, I'm afraid we've broken something");
});
I don't understand how I catch the two arrays in PHP? and use the data from the arrays inside PHP? Preferably I would create new PHP array with them, where the values inside cleanedLinkStructureArray become the keys for the array and the values inside cleanedPermaLinkArray the values for that new array.
I guess it must be something with this, but I need someone more experienced to tell me what I need to do here.
$_POST['cleanedPermaLinkArray[]']
$_POST['cleanedLinkStructureArray[]'];
Any help would be appreciated.
Kind regards,
Marnix
This is all coming down from the server as JSON, right? Just use json_decode
on the values and the'll be converted to arrays, natively inside of php.
$cleanedPermaLinkArray = json_decode($_POST['cleanedPermaLinkArray[]']);
echo cleanedPermaLinkArray[0]; // some value..
Your arrays will be in
$_POST['cleanedPermaLinkArray']
$_POST['cleanedLinkStructureArray'];
you can do a simple var_dump($_POST)
to see how the data is formed
First, in $.post, you do not need the square brackets, so this is one of your params:
'cleanedLinkStructureArray': cleanedLinkStructureArray,
Then in PHP, you have to first catch it like such:
$cleanedLinkStructureArray = $_POST["cleanedLinkStructureArray"];
Now, you can use the following:
foreach ($cleanedLinkStructureArray as $item) {
// Do something with $item
}
Another way is to pass all your params from $.post to php is setting them as json object.