将 PHP 对象传递给 javascript [英] passing PHP objects to javascript
问题描述
我在 php 中有对象,每个对象代表一个项目"以及与之相关的所有信息.
I have objects in php that each represents a "item" and all info associated with it.
当用户浏览页面时,这些对象应该传递给javascript.理想情况下,镜像相同的结构,这样我就可以使用 Raphael 在我的网站上以单独的形状显示每个项目及其信息.
And when the user browses the page, these objects should be passed to javascript. Ideally, mirroring the same structure, so I can use Raphael to display each item and its info as separate shapes on my site.
但是,如何将对象从 php 获取到 javascript?
However, how do you get an object from php to javascript?
推荐答案
您可以将 PHP 对象转换为数组,然后使用 JSON 函数对其进行编码.之后,从 JavaScript 解码它.以下是一些基本步骤:
You can convert the PHP object to an array, and then use JSON function to encode it. After that, decode it from JavaScript. Here are some basic steps:
将 PHP 对象转换为数组.
Convert the PHP object to an array.
使用json_encode()
对该 PHP 数组进行编码.
Using json_encode()
to encode that PHP array.
传递 JSON- 将数据编码为 PHP
Pass JSON-encoded data to PHP
使用 从 JavaScript 解码 JSONJSON.parse
或者你可以使用 jQuery.parseJSON
做到这一点.
Decode JSON from JavaScript using JSON.parse
or you can use jQuery.parseJSON
to do that.
这是一个关于将 JavaScript 对象传递给 PHP 对象的有趣教程一>.通过观看其他一些精选/相关视频,您可能会发现它很有用.
This is an interesting tutorial about passing a JavaScript object to a PHP object. You might find it useful by watching some other featured/related videos.
希望这会有所帮助.
这篇关于将 PHP 对象传递给 javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!