简体   繁体   English

使用和显示 object 数据、PHP 和 json/jquery

[英]using and displaying object data, PHP and json/jquery

I am running into a brick wall when passing object data from jquery to PHP.将 object 数据从 jquery 传递到 PHP 时,我遇到了障碍。 I'm still trying to get my head around OOP.我仍在尝试了解 OOP。

Code follows:代码如下:

        <script type ="text/javascript">
      $(function() { 


    $(".button").click(function() {
          //alert("click");


          var jsonvar1 = {"skillz": {
                            "web":[
                                    {"name": "html", 
                                     "years": "5"
                                    },
                                    {"name": "css", 
                                     "years": "3"
                                    }],
                            "database":[
                                    {"name": "sql", 
                                     "years": "7"
                                    }]
        }};



          $.ajax({
            url: "test2.php",
            type: 'POST',
            data: 'regemail=' + jsonvar1,



                success: function(result) {
                alert(result);

                },
                error:function (xhr, ajaxOptions, thrownError){
                alert(xhr.status);
                alert(thrownError);
            }
            });

      });
      });


        </script>
    </head>
    <body>

<input type="button" id="regSubmit" class="button" value="Register!" /></br></span>
<script>
$('input[type=button]').attr('disabled', false);
    //alert('test');
</script>  

The above code does 3 things.上面的代码做了 3 件事。 Catches a button click, and then: eclares a variable (jsonvar1), and performs an ajax request on that variable to a PHP backend.捕获按钮单击,然后: 声明一个变量 (jsonvar1),并对该变量执行 ajax 请求到 PHP 后端。

Now the code in the PHP backend:现在 PHP 后端的代码:

if (filter_has_var(INPUT_POST, "regemail")) {

    $data = $_REQUEST["regemail"];

    //echo "<br>I got some data!</br>";

    //print_r($data);

    //echo $data->skillz;

    //echo $data;

    var_dump($data);

} else {

echo "No Data.";
}

(safely ignore all the echoes and dumps in the above PHP. That would be me flailing about trying to use the data in some fashion) (安全地忽略上述 PHP 中的所有回声和转储。那会让我对尝试以某种方式使用数据感到不安)

Question: How can I pull data from the object in PHP into variables or an array, or if you prefer, how can I work directly with the values in that object?问题:如何将 PHP 中的 object 中的数据提取到变量或数组中,或者如果您愿意,如何直接使用 object 中的值? (assuming it is an object, and that I'm not making some other unrelated mistake) (假设它是 object,并且我没有犯其他不相关的错误)

I'm wondering if I've neglected to tell my request that it is JSON... http://api.jquery.com/jQuery.getJSON/我想知道我是否忘记告诉我的请求是JSON ...

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Edit: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~ 编辑:

I've attempted to add echo json_decode($data);我试图添加 echo json_decode($data); to my PHP code, but it returns a blank dataset.到我的 PHP 代码,但它返回一个空白数据集。

I also attempted to put dataType: 'json', in my ajax query.我还尝试在我的 ajax 查询中输入 dataType: 'json'。

Still not having any luck it seems.似乎仍然没有任何运气。

You have run into a rather simple error which is easy to overlook: jsonvar1 contains an object, and when you concatenate it to "regemail=" with the + operator, it is turned into a string - but not in the way you intended.您遇到了一个很容易忽略的相当简单的错误:jsonvar1 包含一个 object,当您使用 + 运算符将它连接到“regemail=”时,它会变成一个字符串 - 但不是您想要的方式。 The resulting string is "[object Object]" because JSON conversion is not done automatically for you.结果字符串是“[object Object]”,因为 JSON 转换不会自动为您完成。 Instead, the line should be相反,该行应该是

data: 'regemail=' + JSON.stringify(jsonvar1),

In your PHP file, the line在您的 PHP 文件中,该行

$data = json_decode( $data );

will give you an object to work with, as @sirlancelot has already said.正如@sirlancelot 已经说过的,会给你一个 object 来使用。

You'll want to use json_encode for that on the server side.您需要在服务器端使用json_encode

if (filter_has_var(INPUT_POST, "regemail")) {
    $data = $_REQUEST["regemail"];

    //echo "<br>I got some data!</br>";
    //print_r($data);
    //echo $data->skillz;

    echo json_encode($data);
} else {
    echo "No Data.";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM