从 PHP 提取信息到 JavaScript

发布于 2024-11-01 04:50:30 字数 416 浏览 1 评论 0原文

我已经被困了好几个小时了,试图将一些信息从 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 技术交流群。

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

发布评论

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

评论(2

晨光如昨 2024-11-08 04:50:30

只需从 PHP 构建 HTML/JavaScript 代码...

简单示例 (PHP)

<?php

  $phpvariable = 'test variable from php';

  echo "<html>\n<head>\n</head>\n<body>\n";
  echo "<script type=\"text/javascript\">\n";
  echo "  myPHPvar = '$phpvariable';\n";
  echo "  alert(myPHPvar);\n";
  echo "</script>\n";
  echo "</body>\n</html>";

?>

输出 (HTML)

<html>
<head>
</head>
<body>
<script type="text/javascript">
  myPHPvar = 'test variable set in php';
  alert(myPHPvar);
</script>
</body>
</html>

输出(浏览器)

输入图像描述这里

JavaScript 变量 myPHPvar 从 PHP 变量 $phpvariable 获取值。这是将变量从 PHP 传递到 JavaScript 的最简单方法。

Just build your HTML/JavaScript code from PHP...

Simple example (PHP)

<?php

  $phpvariable = 'test variable from php';

  echo "<html>\n<head>\n</head>\n<body>\n";
  echo "<script type=\"text/javascript\">\n";
  echo "  myPHPvar = '$phpvariable';\n";
  echo "  alert(myPHPvar);\n";
  echo "</script>\n";
  echo "</body>\n</html>";

?>

Output (HTML)

<html>
<head>
</head>
<body>
<script type="text/javascript">
  myPHPvar = 'test variable set in php';
  alert(myPHPvar);
</script>
</body>
</html>

Output (Browser)

enter image description here

JavaScript variable myPHPvar gets value from PHP variable $phpvariable. That is the simplest way to pass variable from PHP to JavaScript.

无人问我粥可暖 2024-11-08 04:50:30

你有什么问题?您想用 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.

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