[英]Javascript stops working after PHP tag
I have set session variable 'activeAcademicyearStartsOn' at the time of login. 我在登录时设置了会话变量'activeAcademicyearStartsOn'。
If we var_dump() the session variable in view.php, the output is like 如果我们在view.php中使用var_dump()会话变量,则输出类似于
object(stdClass)#11 (1) { ["$date"]=> object(stdClass)#12 (1) { ["$numberLong"]=> string(13) "1546297200000" } }
Now in view.php file, I am trying to get session variable in a javascript code inside javascript tag like 现在在view.php文件中,我正在尝试在javascript标签内的javascript代码中获取会话变量,例如
$("#session_start_date").val(getDateString('<?php echo $_SESSION['activeAcademicyearStartsOn'] ?>'));
All the Javascript stops working. 所有的Javascript停止工作。
<script type="..">
...
...
$(document).ready(function(){
...
$("#session_start_date").val(getDateString('<?php echo $_SESSION['activeAcademicyearStartsOn'] ?>'));
...
});
function getDateString(str)
{
if (str == '')
return;
var dateObj = new Date(str.$date.$numberLong - 1000);
var month = dateObj.getMonth() + 1; //months from 1-12
var day = dateObj.getDate();
var year = dateObj.getFullYear();
return (month + "/" + day + "/" + year);
}
</script>
In order to test, if I put another session variable it works fine like 为了测试,如果我放另一个会话变量,它可以像
$("#session_start_date").val('<?php echo $_SESSION['uid'] ?>');
Any help appreciated. 任何帮助表示赞赏。
Seems to me your echoing an object in php, into a quoted string in javascript, and your using it as a object in your function. 在我看来,您将php中的对象回显到javascript中带引号的字符串中,并将其用作函数中的对象。
If you take your php object, and convert it into a javascript object it should work better i think. 如果您将您的php对象,并将其转换为javascript对象,我认为应该会更好。
$("#session_start_date").val(getDateString(<?php echo json_encode($_SESSION['activeAcademicyearStartsOn']); ?>));
You will need to do 2 things 您将需要做两件事
$("#session_start_date").val(getDateString(<?php echo json_encode($_SESSION['activeAcademicyearStartsOn']); ?>));
(This entire peice of code is 1 single line.) (这整个代码是1行。)
str = JSON.parse(str);
str = JSON.parse(str);
inside the getDateString() function. 在getDateString()函数中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.