I have a form that displays a password field and when the form is posted I call a javascript function during the page’s onload — and when I retrieve the password field it never has the password I entered, it’s empty. Is calling a javascript function from onload the wrong approach here if I’m trying access the contents of the posted form?
<!DOCTYPE html>
<html>
<script type="text/javascript">
function checkPwd()
{
// okay the form was posted and we got called from onload, now get the post'd password
var thePwd = document.getElementById("theUsersPassword").value;
alert("the var thePwd is: " + thePwd);
}
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Beta:</title>
</head>
<body onload="checkPwd()">
<form method="post" action="index.php">
Beta: <input id="theUsersPassword" name="usersPassword" type="password"><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
You cannot access the POST data fields from JavaScript.
What you do is that you block the real form submission and get the data from the form.
Or if you’re trying to do some kind of a validation, you need to return a boolean value from
checkPwdand doform.onsubmit = checkPwd