php - 我想在 javascript 代码中调用 php 函数,但出现语法错误

标签 php javascript

POST_manip.html:

<script type="text/javascript">
            // get the post vars
            var $_POST = <?php echo json_encode($_POST); ?>;
            document.write($_POST);

我只是想用 javascript 操作后变量,但首先,我需要这些变量,而 php 是唯一的方法。

最佳答案

json_encode 返回一个有效的 js 对象,而不是字符串,因此您可以将 js $_POST 作为常规对象访问。

.html 文件是通过服务器中的 PHP 进行解析的吗?尝试将文件扩展名更改为 .php

你可以使用console.log($_POST)和类似firebug的东西来代替document.write()来检查变量内容

关于php - 我想在 javascript 代码中调用 php 函数,但出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7122685/

相关文章:

javascript - 使用 javascript 的 ASP 经典站点中的字体颜色不会改变

javascript - Highchart 的选择功能延迟更新

phpmyadmin 错误 : cant create table error number 150

javascript - 反向字符串比较

php - 在页脚标记内关闭MySQL连接

php - 如何确定每个州的中心邮政编码?

javascript - 无法在我的本地 html 中使用地理定位

javascript - 为什么我所需的库没有从 ES6 转换为 ES5?

php - 从 Websocket 服务器中移除 Switch 语句的味道

php - MYSQL查询不会使用php返回数据