最好的方法从PHP数据传递到JavaScript来我的具体情况 [英] Best way to pass data from PHP to JavaScript for my particular case

查看:164
本文介绍了最好的方法从PHP数据传递到JavaScript来我的具体情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立在Drupal在线社区的主页是一种像Facebook的墙。你看最近的25个职位,低于帖子最近的两次意见。还有下方的注释的文本区域,让您可以快速地张贴在一个特定发表新评论。

I've built an online community in Drupal with a homepage that is kind of like the Facebook wall. You see the 25 most recent posts, with the two most recent comments below those posts. There is also a textarea right below those comments so that you can quickly post a new comment on a particular post.

这些Facebook的风格的帖子有很多通过JavaScript内置了功能。点击下方的后一个查看所有评论的链接,使一个AJAX调用,抓住该帖子的所有评论,并显示他们的权利,在它下面。您还可以标记帖子有帮助,作为解决你的问题,行内编辑评论,等等。所有这些行为都需要AJAX请求,这意味着JavaScript的发出请求需要了解的基本信息,如节点ID(唯一职位标识符),注释ID(注释的唯一标识符),等等。

These Facebook-style posts have a lot of functionality built into them via JavaScript. Clicking a "view all comments" link directly below a post makes an AJAX call that grabs all the comments for that post and displays them right below it. You can also mark posts as helpful, as the solution to your question, edit comments inline, etc. All of these actions require AJAX requests, which means that the JavaScript making the request needs to know essential information such as the Node ID (the unique identifier of the post), the comment ID (unique identifier of the comment), etc.

我的初步实施了这些碎片撒遍布岗位必需的数据,使之更加复杂编写需要找到它的JS。所以,我的第二个实现只输出这些数据到在每个帖子的主要包装元素的JSON兼容的字符串。虽然,这使得它更容易为JS找到它所需要的数据,写入JSON作为一个字符串是一个痛苦(逃逸引号,没有换行符)。

My initial implementation had these pieces of essential data sprinkled all over the posts, making it more complicated to write the JS that needed to find it. So my second implementation simply output all this data into a JSON-compatible string in the main wrapping element of each post. While this made it much easier for the JS to find the data it needed, writing JSON as a string is a pain (escaping quotes, no line breaks).

所以,现在我的第三个想法,我之前找的反馈就可以实施。我们的想法是创建一个单一的全球JS数组所有这些职位包含在它的对象保存数据的每个职位。该数组的每个元素将持有所需的AJAX调用必要的数据。所以它看起来是这样的:

So now I have a third idea, and I'm looking for feedback on it prior to implementation. The idea is to create a single global JS Array for all these posts that contains within it objects that hold the data for each post. Each element in that array would hold the necessary data needed for the AJAX calls. So it would look something like this:

Facebook的风格后的模板

Facebook-style post template

<div class="post" data-postindex="<?php echo $post->index; ?>">
    <!-- lots of other HTML for the post -->
</div>
<script type="text/javascript">
    globalPostArray.push({
        nid: <?php echo $post->nid; ?>,
        authorID: <?php $post->authorID; ?>,
        //etc. etc. etc.
    });
</script>

以上code的结果是,当一个链接被点击需要一个AJAX请求,JS会简单地从该链接遍历DOM向上,直到找到主要的。员额元素。然后,它会抓住的数据postindex 的值,以便知道哪些元素 globalPostArray 持有它所需要的数据。

The result of the above code is that when a link gets clicked that requires an AJAX request, the JS would simply traverse the DOM upwards from that link until it finds the main .post element. It would then grab the value of data-postindex in order to know which element in globalPostArray holds the data it needs.

思考?我觉得必须有完成类似这样的一些标准,接受的方式。

Thoughts? I feel like there must be some standard, accepted way of accomplishing something like this.

推荐答案

我从来没有听说过的标准方法PHP和JavaScript之间的通行证的信息,因为他们是一个服务器端和客户端的语言,分别为。我个人用你的第二个和第三个解决方案的混合体。

I've never heard of a standard way to "pass" information between PHP and Javascript, as they are a server-side and client-side language, respectively. I would personally use a hybrid of your second and third solutions.

存储的帖子ID在数据postindex属性(数据属性是新生的,而正确的方式来存储少量数据)。但我仍然只使用一个JSON数组的休息,为存储大量数据的数据属性(和逃避他们!)可能是有问题的。 PHP有一个 json_en code 函数,它负责所有的转义和这样的你 - 只是建立一个PHP数组(例如, $ POSTDATA )像往常一样,然后抛出此在您的文章模板:

Store the post id in a data-postindex attribute (data attributes are newish, and the "right" way to store small amounts of data). But I would still just use a JSON array for the rest, as storing lots of data in data-attributes (and escaping them!) is potentially problematic. PHP has a json_encode function that takes care of all the escaping and such for you - just build a PHP array (say, $postdata) like you normally would, and then throw this in your post template:

<script type="text/javascript">
    globalPostArray.push(<?php echo json_encode($postdata) ?>);
</script>

其中, $ POSTDATA 是类似如下:

$postdata = array(
    'nid' => 5,
    'authorId' => 45
    ...etc...
);

这应该很容易从现有的code产生这样的数组。

It should be easy enough to generate such an array from your existing code.

我写了一个博客帖子前阵子我的实现这种事情,但它听起来像你需要的是一个指针的 json_en code

I wrote a blog post a while back about my implementation of this kind of thing, but it sounds like all you need is a pointer at json_encode.

这篇关于最好的方法从PHP数据传递到JavaScript来我的具体情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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