将 JSON 存储在隐藏的输入元素中?

发布于 2024-09-08 02:35:13 字数 371 浏览 2 评论 0原文

我需要能够有效地生成无限数量的数据集,所以我想做的是这样的;

<input type="hidden" name="items[]" value="{id:1,name:'some-name'}" />

我尝试 JSON.stringify 在 javascript 中转换我的数组并将其存储在当前隐藏的输入元素中,但它将所有键和值都用双引号括起来,这显然与 HTML 将整个值用双引号括起来相冲突。不知何故,我需要转义引号,但我需要它以两种方式工作...基本上这些元素是用 PHP 生成的并按顺序放置在页面中,然后我可以在用户端添加或删除项目并提交页面, PHP 应该迭代这些隐藏元素并更新记录。

有人对某种转义方法有什么建议吗?

I need to be able to generate an effectively unlimited number of datasets, so what I want to do is something like this;

<input type="hidden" name="items[]" value="{id:1,name:'some-name'}" />

I tried JSON.stringify to convert my array in javascript and store it in the current hidden input element, but it wraps all the keys and values in double quotes, which obviously conflicts with HTML wrapping the entire value in double quotes. Somehow I need to escape the quotes, but I need this to work two ways...basically these elements are generated with PHP and placed in the page in order, then I can add or delete items on the user end and submit the page, which should have PHP iterating through these hidden elements and updating the records.

Anyone have any suggestions on an escaping method of some sort?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦旅人picnic 2024-09-15 02:35:13

您可以使用此处提供的转义函数: http://phpjs.org/functions/htmlspecialchars:426

它应该转义 json 中的字符,并使您的字符串可以安全地用作 html 属性的值。

如果你想在 PHP 上进行转义,那么你应该使用 PHP 中内置的函数 htmlspecialchars() 。

You can use escaping function presented here: http://phpjs.org/functions/htmlspecialchars:426

It should escape chars in json and make your string safe to use as value of html attribute.

If you want to do the escaping on PHP then you should use function htmlspecialchars() that is built in PHP.

煮茶煮酒煮时光 2024-09-15 02:35:13

你想要做的是掌握一些 html5 data-* 属性,这样你就可以做到

<div id="post-container" data-meta="{id:22,name:'Robert Pitt'}">
   ..
</div>

然后你可以使用 htmlentites() 来使字符串安全并使用 javascript,你可以使用 javascript 获取数据,如下所示:

function ElementJson(id,attrib)
{
    var post_c = document.getElementById(id);

    for( var x = 0; x < post_c.attributes.length; x++)
    {
        if( post_c.attributes[x].nodeName.toLowerCase() == 'data-' + attrib.toLowerCase())
        {
            return post_c.attributes[x].nodeValue;
        }
    }
    return false;
}
json = ElementJson('post-container','meta');

例如通过 jQuery 你可以 都使用它,尤其是 Facebook

json = $('#post-container[data-meta]').attr('data-meta');

许多大型网站

What you want to do is grasp some of html5 data-* attrubites so you can dod

<div id="post-container" data-meta="{id:22,name:'Robert Pitt'}">
   ..
</div>

Then you can use htmlentites() to make the string safe and use javascript, you can get the data with javascript like so:

function ElementJson(id,attrib)
{
    var post_c = document.getElementById(id);

    for( var x = 0; x < post_c.attributes.length; x++)
    {
        if( post_c.attributes[x].nodeName.toLowerCase() == 'data-' + attrib.toLowerCase())
        {
            return post_c.attributes[x].nodeValue;
        }
    }
    return false;
}
json = ElementJson('post-container','meta');

Via jQuery for instance you can do

json = $('#post-container[data-meta]').attr('data-meta');

A lot of large sites use it especially Facebook

骑趴 2024-09-15 02:35:13

对数据进行 base64 编码或通过 htmlentities() 运行它以将引号转换为实体:)

base64_encode the data or run it through htmlentities() to turn the quotes in to entities :)

淡淡绿茶香 2024-09-15 02:35:13

老实说,如果您在页面中的 javascript 中设置值,那么我对您遇到的问题感到有点惊讶,但这里有一些建议: -

当然,正确的做法是HTML 对 html 属性中的数据进行编码 - 但这需要调用 htmlentities 或类似的方法,所以为什么不使用“内置”javascript encodedecode 对数据进行 URL 编码方法。 URL 编码应将双引号转义为“%22”。

或者,如果您已经在使用 jQuery,请使用 val() 方法来设置(并获取?)该值 - 我相信这会为您解决这些问题(尽管我还没有去,实际上检查了这个)。

To be honest, if you are setting the value in javascript in the page then I am a little surprised that you ran into problems but here are a couple of suggestions: -

Of course the proper thing to do is HTML encode the data in the html attributes - but this requires calling htmlentities or similar so instead, why not URL encode the data using the "built in" javascript encode and decode and methods. URL encoding should escape double quotes to '%22'.

Or if you are already using jQuery, use the val() method to set (and get?) the value - I am sure that would deal with these issues for you (although I have not gone and actually checked this).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文