I am trying to do a basic ajax login but for the life of me I cant figure out why the uname and pword variables keep printing [object HTMLInputElement]
when I do toString()
You can see a working example here
Just type something in and click login. It will hit the db, then print out username, password, then server response
<script>
function login() {
var xmlhttp;
var uname = "";
var pword = "";
var date;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
uname = document.getElementById("username").toString();
pword = document.getElementById("password").toString();
date = new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString();
xmlhttp.open("GET", "login.php?uname=" + uname.toString() + "&pword=" + pword.toString() + "&date=" + date, true);
xmlhttp.send();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("response").innerHTML = uname.toString() + "<br/>" + pword.toString() + "<br/>" + date + "<br/>" + xmlhttp.responseText;
}
}
}
</script>
This is the HTML Code
<body>
<center>
<div style="align-content:center">
<form>
<br />
<br />
<br />
<br />
<h1 style="font-family:'Microsoft Yi Baiti'; font-size:40px">Ajax test Site</h1>
<br />
<label>Username</label>
<br />
<input type="text" id="username" name="username" style="width:190px"/>
<br />
<label>Password</label>
<br />
<input type="password" id="password" name="password"style="width:190px"/>
<br />
<label id="tryAgainText" style="color:red; visibility:hidden">Username or Password incorrect</label>
<br />
<input onclick="login()" type="button" name="submit" value="Login" style="width:216px; margin-left: 0px;"/>
</form>
<p id="response">This is the response field</p>
</div>
</center>
</body>