从 PHP 提取信息到 JavaScript
我已经被困了好几个小时了,试图将一些信息从 PHP 脚本传递到 JavaScript。 PHP 脚本回显 HTML,我认为如果我尝试通过 AJAX 向 JavaScript 发送 JSON 对象,这可能会出现问题。问题是,所有数组等都是在 PHP 文件中创建的,它会回显结果,所以我所做的是尝试对这些数组进行 json_encode/和/或以某种方式回显它们。
我是否可以通过这种方式发送信息?我唯一能想到的就是将这些数组写入一个新文件,然后将其分解为新的和类似的。这不是一个很有趣的想法。
让您了解我的 PHP 是什么样子:
echo "<html here>";
echo "<html here>";
echo "<html here>";
echo json_encode($array);
I've been stuck for hours now trying to pass a few pieces of information from a PHP script to JavaScript. The PHP script echoes out HTML and I've figured there might be a problem with this if I'm to try and send the JavaScript a JSON object through AJAX. The thing is, all the arrays and such are made within the PHP file which echoes out the results, so what I have done is to try and json_encode those arrays/and or echo them out somehow.
Is it even possible for me to send the information this way? The only thing I can think of would be to write those arrays into a new file and then explode it up into new ones and similar. Not a very fun thought.
To give you an idea of what my PHP looks like:
echo "<html here>";
echo "<html here>";
echo "<html here>";
echo json_encode($array);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需从 PHP 构建 HTML/JavaScript 代码...
简单示例 (PHP)
输出 (HTML)
输出(浏览器)
JavaScript 变量 myPHPvar 从 PHP 变量 $phpvariable 获取值。这是将变量从 PHP 传递到 JavaScript 的最简单方法。
Just build your HTML/JavaScript code from PHP...
Simple example (PHP)
Output (HTML)
Output (Browser)
JavaScript variable myPHPvar gets value from PHP variable $phpvariable. That is the simplest way to pass variable from PHP to JavaScript.
你有什么问题?您想用 PHP 中的数据在 javascript 中实现什么目的? ajax 调用是否有效?
一般来说,我建议将 HTML 构造与以 json 形式提供数组数据分开。
您想在一次 PHP 调用中处理这两个问题,不是吗?
您可以将 html 部分附加到数组并仅发送 json,或者将数组数据放在假 html 元素中,稍后您可以将其从显示中排除。取决于两个部分的复杂程度。请提供更多示例代码和信息。
What's your question? What are you trying to achieve in javascript with your data from PHP? Does the ajax call work at all?
In general I'd recommend separating HTML construction from providing array data as json.
You'd like to handle both in one single PHP call, don't you?
You could attach html parts to you array and send json only, or have array data in a fake html element, that you might exclude from display later. Depends on how complex both parts are. Give some more example code and information, please.