无法登录总是失败 [英] Cant Login always failed
问题描述
这里是user.xml代码
user.xml
<用户>
< user>
< username> A< / username>
< password> 12345< / password>
< / user>
< / users>
尊重login.html代码
<!DOCTYPE HTML>
< html lang =en-US>
< head>
< / head>
< body>
< form id =frmloginname =frmloginmethod =postaction =Loginonsumbit =return false;>
< p> ENTER USER NAME< input type =textname =login_usernameid =username>< / p>
< p> ENTER PASSWORD< input type =passwordname =login_passid =password>< br>< br>
< input type =buttonclass =buttonvalue =登录onclick =login()>
< / p>
< / form>
< script>
window.login = function(e)
{
if(document.frmlogin.login_username.value ==)
{
alert 用户名不为空);
返回;
}
else if(document.frmlogin.login_pass.value ==)
{
alert(Password is not blank);
返回;
}
else
{
var xmlDoc;
if(window.XMLHttpRequest){
xhttp = new XMLHttpRequest();
} else {// IE 5/6
xhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xhttp.overrideMimeType('text / xml');
xhttp.open(GET,user.xml,false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
var ktra = true;
var xml = xmlDoc.childNodes [0] .childNodes [1];
var name = xml.childNodes [username];
var pass = xml.childNodes [password];
(var i = 0; i< xml.childNodes.length; i ++){
if(xml.childNodes [i] .nodeName =username)
name = xml.childNodes [一世];
if(xml.childNodes [i] .nodeName =password)
pass = xml.childNodes [i];
}
if(name.textContent == frmlogin.login_username.value&& pass.textContent == frmlogin.login_pass.value)
{
ktra = true;
}
else
{
ktra = false;
}
if(ktra == true)
{
alert(登录成功!!!);
}
else
{
alert(登录失败!!!);
}
}
}
< / script>
< / body>
< / html>
您正在将字符串与=进行比较。尝试使用 xml.childNodes [i] .nodeName ==username
和密码相同。也不要用不同于英语的其他语言来命名变量。很难读
Good day I am a newbie in webdev. Im trying to create a project where in there is a login form that users will input their username and password.Whenever the button is click it will display a message Login successfully and if the username and password is not stored in the xml it will display Login Failed!!.Can someone tell me what is the mistake of the code?Login keeps having alert message Login Failed when button is click.Below is the code.Thanks in advance for help.
here is the user.xml code
user.xml
<users>
<user>
<username>A</username>
<password>12345</password>
</user>
</users>
heres the login.html code
<!DOCTYPE HTML>
<html lang="en-US">
<head>
</head>
<body>
<form id="frmlogin" name="frmlogin" method="post" action="Login" onsumbit="return false;">
<p>ENTER USER NAME <input type="text" name="login_username" id="username"></p>
<p> ENTER PASSWORD <input type="password" name="login_pass" id="password"><br><br>
<input type="button" class="button" value="Log in" onclick="login()">
</p>
</form>
<script>
window.login = function(e)
{
if (document.frmlogin.login_username.value == "")
{
alert("User name is not blank");
return;
}
else if(document.frmlogin.login_pass.value == "")
{
alert("Password is not blank");
return;
}
else
{
var xmlDoc;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else { // IE 5/6
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.overrideMimeType('text/xml');
xhttp.open("GET", "user.xml", false);
xhttp.send(null);
xmlDoc = xhttp.responseXML;
var ktra=true;
var xml=xmlDoc.childNodes[0].childNodes[1];
var name = xml.childNodes["username"];
var pass=xml.childNodes["password"];
for(var i=0;i<xml.childNodes.length;i++){
if(xml.childNodes[i].nodeName="username")
name = xml.childNodes[i];
if(xml.childNodes[i].nodeName="password")
pass = xml.childNodes[i];
}
if(name.textContent==frmlogin.login_username.value &&pass.textContent==frmlogin.login_pass.value)
{
ktra=true;
}
else
{
ktra=false;
}
if(ktra==true)
{
alert("Login Successfully !!!");
}
else
{
alert("Login Failed !!!");
}
}
}
</script>
</body>
</html>
You are comparing strings with "=". Try with xml.childNodes[i].nodeName=="username"
and the same for the password if. Also do not name variables in other languages different than English. Its hard to read.
这篇关于无法登录总是失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!