在Javascript中正确格式化JSON数组 [英] Properly format JSON array in Javascript

查看:119
本文介绍了在Javascript中正确格式化JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的删除功能.此函数删除PHP正在输出的列表中的条目.这就是发生的情况:

I'm writing a simple delete function. This function deletes entries in a list that is being outputted by PHP. This is what happens:

  1. javascript选中此列表中的复选框,并获取其ID 1,2,3etc.
  2. 使用JSON.stringify将创建的数组转换为JSON
  3. PHP获取POST并使用json_decode()对其进行解码(失败)

$(document).ready(function(){

    $('.action_trash').on('click', function(){

        //Select items to trash
        var TrashItems = [];
        $('input:checked').each(function() {

            TrashItems.push($(this).attr("id"));
        });
        //Convert to JSON
        var TrashJSON = JSON.stringify(TrashItems);
        alert (TrashJSON);
        $.ajax({
            type: "POST",
            url: "pages/forms_intake/actions/trash/model_trash.php",
            data: {TrashItems:TrashItems}, 
            cache: false,

            success: function(){
                // do stuff to remove trashed elements from list.
            }
        }); 
    });     
});

我正在尝试将简单的Javascript数组转换为JSON.但我认为JSON无效.当我提醒TrashJSON变量时,输出为:["31","32"]

I'm trying to convert the simple Javascript array to JSON. But I think the JSON is invalid. When I alert the TrashJSON variable, this is the output: ["31","32"]

<?php
session_start();
include "../../../../php/config.php";

$TrashJSON = $_POST['TrashItems'];
var_dump(json_decode($TrashJSON));

//Trash items
//$TrashQuery = mysqli_query ($mysqli, "UPDATE forms_intake SET item_trashed=1 WHERE formid='$TrashInt'"); 

//Close connection
mysqli_close($mysqli);
?>

运行此命令时,出现此错误:

When I run this, I'm getting this error:

<br />
<b>Warning</b>:  json_decode() expects parameter 1 to be string, array given in <b>/home/nickvmf103/domains/a-training.nl/public_html/sotwebapp/pages/forms_intake/actions/trash/model_trash.php</b> on line <b>7</b><br />
NULL

根据JSON_decode上的PHP手册,如果JSON无效,则输出null.

According to the PHP manual on json_decode, null gets outputted if the JSON is invalid.

所以我的问题是:如何在我的JavaScript中正确格式化JSON?这样就可以将其传递给PHP并转换为PHP数组.

So my question is: How to correctly format the JSON in my javascript? So that it can be passed to PHP and converted to a PHP array.

其他信息: 成功转换为PHP数组后,我将使用implode将"OR"放在数组值之间,将其转换为字符串,然后在SQL查询WHERE语句中使用它来丢弃页面上已检查的项目.

Additional info: When successfully converted to PHP array, I will use implode to put " OR " between the array values, convert it to a string and use it in my SQL query WHERE statement to trash the items that were checked on my page.

推荐答案

为了使现有的PHP发挥预期的作用,您需要发送TrashJSON而不是TrashItems. TrashItems是您填充的数组,而TrashJsonTrashItems的JSON编码版本.

In order for the existing PHP to function how you expect, you need to send TrashJSON instead of TrashItems. TrashItems is the array you've populated, while TrashJson is the JSON encoded version of TrashItems.

更改

data: {TrashItems:TrashItems},

到..

data: {TrashItems:TrashJSON},

jQuery正确地将TrashItems解释为数组,并因此将其作为值数组发送到PHP脚本.

jQuery is correctly interpreting TrashItems as an array, and thus sending it as an array of values to the PHP script.

或者,正如Barmar在评论中推断的那样,您可以简单地遍历当前发布为TrashItems的values数组.您可以使用以下代码执行此操作.

Alternatively, as Barmar has inferred in the comments, you could simply loop over the values array currently posted as TrashItems. You could do this using the following code.

foreach ($_POST['TrashItems'] as $TrashItem) {
    echo "$TrashItem\n";
}

这将输出您最初在单独的行中传递的TrashItems数组的每个项目,并提供输出..

This would output each item of the TrashItems array you originally passed in on a separate line, and provide the output of..

31
32

这篇关于在Javascript中正确格式化JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆