[英]Cannot get value from PHP to Javascript
因此,在我的PHP文件中,我创建了一个JSON对象,我需要将其转到Javascript文件进行处理。 但是,我似乎无法传输任何内容,即使使用这样的简单字符串也是如此:
的index.php
<?php
$cookie= "blueberry cake";
?>;
的script.js
var details = "<?php echo $cookie; ?>";
function myFunction() {
document.getElementById("demo").innerHTML = details;
}
的index.html
<html>
<head>
<script src="script.js"></script>
<title>antgus</title>
</head>
<body>
<h1>Welcome to my page! </h1>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo">A Paragraph.</p>
</body>
我在HTML中有一个按钮,因此当我按下按钮时,就会调用myFunction get函数。 应该将ID为“ demo”的ap元素更改为字符串“ blueberry cake”
知道我在做什么错吗?
编辑
我现在可以正常工作,除了一件事,我无法将JSON对象传递给Javascript函数。 控制台抛出一个错误,指出参数错误,它认为我正在传递多个字符串。 的index.php
<?php
$test = "blue";
$logLines = file('../../../home/shares/flower_hum/humid.log');
$entries = array_map("clean",$logLines);
$finalOutput = [
'humid' => $entries
];
function clean($string){
return json_decode(rtrim(trim($string),','),true);
}
$json = json_encode($finalOutput, JSON_UNESCAPED_SLASHES);
echo $json; //displays valid JSON, I have checked it.
?>
<html>
<head>
<script src="script.js"></script>
<title>antgus</title>
</head>
<body>
<p>Welcome to my page! Down below you can see the data:</p>
<button type="button" onclick='myFunction("<?php echo $json ?>")'>Try it</button>
<p id="demo">A Paragraph.</p>
</body>
</html>
的script.js
function myFunction(jsonObject) {
document.getElementById("demo").innerHTML = "INCOMING DATA:";
alert(jsonObject);
}
enter code here
外部js文件不会被解析为php,这意味着您无法访问该变量。 您可以将js保存为php(不推荐)。 或者a)
在php页面底部的底部添加该函数:
<?php
$cookie= "blueberry cake";
?>;
//js further down the same page
<script>
var details = "<?php echo $cookie; ?>";
function myFunction() {
document.getElementById("demo").innerHTML = details;
}
</script>
或b)在php页面中声明一个全局变量,以便外部js可以访问它:
//php page
<?php
$cookie= "blueberry cake";
?>;
<script> var details = "<?php echo $cookie; ?>";</script>
//js file
function myFunction() {
//details will be accessible since it has been declared in the global space of the index.php page
document.getElementById("demo").innerHTML = details;
}
这应该工作完美:
使用文档内JS:
<?php
$cookie= "blueberry cake";
?>;
<html>
<head>
<script type="text/javascript">
var details = "<?php echo $cookie; ?>";
function myFunction() {
document.getElementById("demo").innerHTML = details;
}
</script>
<title>antgus</title>
</head>
<body>
<h1>Welcome to my page!</h1>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo">A Paragraph.</p>
</body>
使用外部JS:
<?php
$cookie= "blueberry cake";
?>;
<html>
<head>
<script type="text/javascript">
var details = "<?php echo $cookie; ?>";
</script>
<script type="text/javascript" src="script.js"> </script>
<title>antgus</title>
</head>
<body>
<h1>Welcome to my page!</h1>
<button type="button" onclick="myFunction()">Try it</button>
<p id="demo">A Paragraph.</p>
</body>
script.js文件:
function myFunction() {
// HAS ACCESS TO details WHICH IS ON THE GLOBAL SCOPE
document.getElementById("demo").innerHTML = details;
}
使用json_encode
编码json对象,即
<script> var details = "<?php echo json_encode($cookie); ?>";</script>
要考虑的事情:
1,外部js无法像您尝试的那样工作。
2.当您直接通过onclick
代码调用javascript函数时,还必须传递数据以进一步工作。
工作代码示例:-
index.php文件: -
<?php
$cookie= "blueberry cake";
?>
<html>
<head>
<script src="script.js"></script>
<title>antgus</title>
</head>
<body>
<h1>Welcome to my page! </h1>
<button type="button" onclick='myFunction("<?php echo $cookie; ?>")'>Try it</button>
<p id="demo">A Paragraph.</p>
的script.js: -
function myFunction(details) {
document.getElementById("demo").innerHTML = details;
}
输出在我的本地端:
点击之前: -http : //prntscr.com/ci94s9
单击后: -http : //prntscr.com/ci94w9
删除php周围的引号,老兄。 您正在分配字符串而不是php变量值。
var details = "'" + <?php echo $cookie; ?> + "'";
这将分配实际的$cookie
值,而不是字符串'<? php echo $cookie; ?>'
'<? php echo $cookie; ?>'
'<? php echo $cookie; ?>'
。
php-script在服务器端运行,而javascript在客户端运行,两者不共享某种范围。
换句话说, $cookie
变量在php中分配了一个值,但未使用。 然后,您在javascript中使用具有相同名称的变量(但与php变量完全无关),但是由于从未分配任何变量,因此undefined
。
gavgrif提供了一些解决方案来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.