【发布时间】:2017-02-23 01:06:11
【问题描述】:
我一直在尝试使用这个 javascript 中的“用户”变量,以便可以在我的 php 脚本中使用它。我想在 php 脚本中回显用户变量。我无法这样做,因为我一直在尝试回应 $user; .我也无法在互联网上找到任何帮助我解决这个问题的东西。
alert("Welcome again " + user);
我希望在屏幕上打印而不是警告再次欢迎用户(用户名) 谢谢你
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(cname,cvalue,exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user=getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:","");
if (user != "" && user != null) {
setCookie("username", user, 30);
}
}
}
</script>
<?php echo "Welcome back : ".$user;?>
</head>
<body onload="checkCookie()">
</body>
</html>
【问题讨论】:
-
请解释一下您所说的回声是什么意思? echo 是一个 PHP 命令,而 alert 是 javascript。两人永远不会相遇
-
很抱歉我已经编辑了我的问题
标签: javascript php html css cookies