I have sent data using AJAX. I have used this code. I can handle & recive productInfo:JSON
data using PHP but can't be handling datastring
. Because showing like below image. How can handle or receive datastring
using PHP?
var datastring = jQuery("#chackOutBillingIfor").serialize();
jQuery.ajax({
url: "/wp-admin/admin-ajax.php?action=chackOut",
data: {
productInfo:JSON.stringify(cart),
billingInfo:JSON.stringify(datastring),
},
type: 'POST',
cache: false,
})
You can split the billingInfo
string on the &
to get a list of key and value strings and then split those strings on =
. The following function should be sufficient to return an array from the value.
function stringToArray($str)
{
$result = array();
foreach (explode('&', $str) as $pair) {
list ($key, $val) = explode('=', $pair);
$result[$key] = $val;
}
return empty($result) ? false : $result;
}
You may want to perform a check that the string contains at least one =
and it cannot be in first position before processing the string.