抱歉,我对编程特别是java脚本很陌生。请帮我。
我正在使用 php 从数据库获取和更新数据。我尝试使用 JavaScript 函数 onclick
从数据库获取数据,但我不断收到相同的错误。
首先是我已经获取了数据。
并将其存储在我的变量 $data
中,但我按照我观看的视频教程应用了 json_encode
,但不幸的是我的代码不起作用。我不知道为什么。
这就是我启动代码的方式:
//This is foreach loop with a variable of $user where all the data has been stored in array(I'm not sure), before the line of this code below.
$data = json_encode($user, true);
//In my link/href where I'm getting an error that my $data is not defined.
<a href='javascript:getUpdateUser($data);' id='edit'> EDIT </a>
//and my script
<script>
function getUpdateUser(user) {
alert(user);
}
</script>
最佳答案
您需要返回 PHP 模式才能回显该变量。
<a href='javascript:getUpdateUser(<?php echo $data; ?>);' id='edit'> EDIT </a>
此外,json_encode()
的第二个参数不是 bool 值,而是包含标志的整数。您将其与 json_decode() 混淆了,后者使用第二个参数来确定是返回对象还是关联数组。当您设置 $data
时,它应该是:
$data = json_encode($user);
关于javascript - Uncaught ReferenceError : $data is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55080759/