【问题标题】:JavaScript object data extraction get data from json.stringify to phpJavaScript 对象数据提取从 json.stringify 获取数据到 php
【发布时间】:2014-11-18 09:39:05
【问题描述】:

我正在尝试使用 json.stringify 发布数据
像这样的数据

    <script>   
     $('.create-invoice').on('click', function()
            {
                grab_invoice_data();
                // Declare a variable
                var jsonObj = invoice_data;

                // Lets convert our JSON object
                var postData = JSON.stringify(jsonObj);

                // Lets put our stringified json into a variable for posting
                var postArray = {json: postData};

                $.download("json.php", postArray, 'post');`enter code here`
            })

    </script>    

// php has like this 

   <?php
$arrData = json_decode($_POST['json'],true);
extract($arrData , EXTR_PREFIX_SAME, "wddx");

$firstName = $arrData['name'];
$address1 = $arrData['address1'];
$address2 = $arrData['address2'];
$city = $arrData['city'];
$state = $arrData['state'];
echo $firstName;
echo $address1;
echo $address2;
?>

注意:未定义索引:第 7 行 C:\xampp\htdocs\ASK_Soft\json.php 中的名称

注意:未定义索引:第 8 行 C:\xampp\htdocs\ASK_Soft\json.php 中的地址1

注意:未定义索引:第 9 行 C:\xampp\htdocs\ASK_Soft\json.php 中的地址 2

注意:未定义索引:第 10 行 C:\xampp\htdocs\ASK_Soft\json.php 中的城市 . . 谢谢。

【问题讨论】:

    标签: php json


    【解决方案1】:

    在您的 php 脚本中编写以下代码

    $arrData = json_decode($youdata,true); // this will give you data in array format
    
    extract($arrData , EXTR_PREFIX_SAME, "wddx");
    

    现在你可以..

    echo $address1;
    echo $address2; //and so on
    

    【讨论】:

    • 要参考提取功能请转到php.net/manual/en/function.extract.php URL
    • 你这一行 $data = json_decode(file_get_contents('php://input'), true); “$data”变量
    • 你的行 json_decode(file_get_contents('php://input'), true);给你空数据。它应该给你一个数组
    • 用 $_POST['json'] 替换这个 file_get_contents('php://input')
    • 用 $_POST['json'] 替换这个 file_get_contents('php://input')
    【解决方案2】:

    json_decode给你一个对象,所以:

    $firstName = $data->{'name'};
    $address = $data->{'address'};
    

    php.net - json_decode

    【讨论】:

      【解决方案3】:

      经过多次尝试,我得到了答案

         <?php
      $arrData = json_decode($_POST['json'],true);
      extract($arrData , EXTR_PREFIX_SAME, "wddx");
      
      $firstName = $arrData['name'];
      $address1 = $arrData['address1'];
      $address2 = $arrData['address2'];
      $city = $arrData['city'];
      $state = $arrData['state'];
      echo $firstName;
      echo $address1;
      echo $address2;
      ?>
      

      感谢 Lalit Sharma 的回答

      【讨论】:

        猜你喜欢
        • 2015-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多