【发布时间】:2012-11-29 14:14:44
【问题描述】:
我正在为学校做一个项目,但我正在为登录页面而苦恼。在我的 PHP 代码中,如果用户输入了错误的用户名或密码,我想调用一个 Javascript 函数来显示一条消息并短暂更改显示消息的背景颜色。这是我的 JS 和 PHP 代码块:
<script>
var flashContent = function () {
document.getElementById("outputlogin").style.backgroundColor = "#ffff00";
document.getElementById("outputlogin").innerHTML = "Incorrect login.";
function proxy() {
updateColor(0);
}
setTimeout(proxy, 50);
}
var updateColor = function (newColor) {
var hexColor = newColor.toString(16);
if (hexColor.length < 2)
hexColor = "0" + hexColor;
var colorString = "#ffff" + hexColor;
document.getElementById("outputlogin").style.backgroundColor = colorString;
function proxy() {
updateColor(newColor);
}
if (newColor < 255) {
newColor = newColor + 5;
setTimeout(proxy, 50);
}
}
</script>
<?php
$username = $_POST['username'];
$password = $_POST['password'];
if(($username == "Ben") && ($password == "thepassword")){
//echo "SUCCESS";
session_start();
$_SESSION['bensedmgallerysesh'] = session_id();
header("Location:../index.php");
}else{
if($username != "" && $password != ""){
javascript:flashContent();
}
}
?>
现在,点击登录按钮后,我收到错误消息:
致命错误:调用未定义的函数 flashContent()
我该如何解决这个问题?
【问题讨论】:
-
您还想查看this question over on Meta,了解您名下的大肥橙“0%”可能会如何影响您获得答案的能力。
-
您使用 Javascript 作为客户端语言,使用 PHP 作为服务器端语言。您不能从另一种语言中直接调用其中一种语言的方法。
-
你知道什么 JS 和 PHP 在完全不同的环境中运行吗?一种是客户端(浏览器),JS,一种是服务器端,PHP。如果您需要一个 PHP 脚本来验证您的表单服务器端,那么您有两个选择,向 PHP 脚本发出 AJAX 请求并让 JS 采取相应的行动,或者让表单提交给 PHP 脚本并让它输出一些 HTML/ JS 会做你需要做的事情。彼此之间没有呼唤。
标签: php javascript