weixin_33716154 2019-02-04 05:53 采纳率: 0%
浏览 14

通过PHP处理AJAX数据

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? enter image description here

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,
})
  • 写回答

1条回答 默认 最新

  • weixin_33697898 2019-02-04 12:47
    关注

    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.

    评论

报告相同问题?