Javascript JS脚本函数在php中不起作用
以下是我的JS函数:Javascript JS脚本函数在php中不起作用,javascript,php,function,Javascript,Php,Function,以下是我的JS函数: function checkError() { var field = 'error'; var url = window.location.href; document.write('test'); window.alert('please work'); if(url.indexOf('?' + field + '=') != -1)
function checkError() {
var field = 'error';
var url = window.location.href;
document.write('test');
window.alert('please work');
if(url.indexOf('?' + field + '=') != -1)
document.write('The username and password do not match. Do not use your full email.');
return true;
}
在我的正文部分,我有:
<?php echo '<script> checkError();</script>' ?>
调用它时没有任何错误。但是这个函数在我的页面上没有任何作用。有什么想法吗?我尝试将JS脚本放在页面和JS文件中,并正确地调用了它
完整脚本:
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login</title>
<script type="text/JavaScript" src="js/functions.js"></script>
<link href="stylesheets/mainStyle.css" rel="stylesheet" type="text/css">
<link href="stylesheets/formStyle.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php include('header.php'); ?>
<div id="mainContent">
<h1>Member Login</h1>
<div id="mainParaText">
<?php echo '<script> checkError();</script>' ?>
</div>
</body>
</html>
安全登录
会员登录
原来JS函数是未定义的。呃,不明白为什么(我想我刚才解决了这个问题,哈哈)JS是客户端,PHP是服务器端 这意味着PHP代码中的所有内容都将在服务器上处理,然后“回显”到您的浏览器,在那个里您将得到未定义的错误 调试时,您应该始终首先检查浏览器中的源代码,以便准确地看到服务器响应的内容
我猜一点,但试着用双引号而不是单引号 与问题无关。。。 您正在用JS检查用户名和密码?你到底要怎么做?使用ajax回调服务器?如果您使用JS进行检查,这意味着密码应该在源代码中的某个地方,这是不安全的。用户名/密码验证应始终在服务器端进行(使用ajax请求或通常的提交表单) 试着这样做:
安全登录
函数checkError(){
变量字段='错误';
var url=window.location.href;
文件。编写(“测试”);
window.alert(“请工作”);
如果(url.indexOf(“?”+字段+“=”)!=-1)
document.write('用户名和密码不匹配。不要使用完整的电子邮件');
返回true;
}
会员登录
显示此脚本的完整用法。控制台中有错误吗?请查看浏览器的开发人员工具。看看网络标签。是否正在请求脚本?它得到回应了吗?它们是否包含您期望的数据?这是JS文件中唯一的函数吗?您似乎没有包含所有内容(但我希望控制台中会显示更多错误)。“我猜一点,但尝试使用双引号而不是单引号。”-鉴于我们可以看到的代码,这可能会有什么不同?如果您查看函数,我会检查url中的查询语句,如果有错误,我将用户重定向到带有错误查询的登录页面
<html>
<head>
<meta charset="UTF-8">
<title>Secure Login</title>
<script type="text/JavaScript" src="js/functions.js"></script>
<link href="stylesheets/mainStyle.css" rel="stylesheet" type="text/css">
<link href="stylesheets/formStyle.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function checkError(){
var field = 'error';
var url = window.location.href;
document.write('test');
window.alert('please work');
if(url.indexOf('?' + field + '=') != -1)
document.write('The username and password do not match. Do not use your full email.');
return true;
}
<?php
echo "checkError();";
?>
</script>
</head>
<body>
<?php include('header.php'); ?>
<div id="mainContent">
<h1>Member Login</h1>
<div id="mainParaText">
<?php echo '<script> checkError();</script>' ?>
</div>
</body>
</html>