Reputation: 1827
In my webpage, I try to send parameters by JavaScript.
When I get the request, I find the parameters is null? I am quite sure , the request can be sent successfully because I can receive the request.
I don't know why it is wrong, this is my code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta http-equiv="Expires" content="0">
<meta http-equiv="kiben" content="no-cache">
<title>Login Page</title>
<script type="text/javascript">
function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = window.document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (window.document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = window.document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function getCookieVal (offset)
{
var endstr = window.document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = window.document.cookie.length;
return unescape(window.document.cookie.substring(offset, endstr));
}
function SetCookie (name, value)
{
var exp = new Date();
exp.setTime(exp.getTime() + (30*24*60*60*1000));
window.document.cookie = name + "=" + escape (value) + "; expires=" + exp.toGMTString()+";path=/";
}
function DeleteCookie (name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 100);
var cval = GetCookie (name);
window.document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString()+";path=/";
}
function DelCookie()
{
DeleteCookie(document.getElementById("username").value);
}
function remember()
{
if(document.getElementById("remember").checked){
SetCookie(document.getElementById("username").value,document.getElementById("password").value);
alert("Saved!");
}
createForm();
}
function showpassword()
{
var p=GetCookie(document.getElementById("username").value);
if(p!=null)
document.getElementById("password").value= p;
}
function createForm(){
var form = document.createElement("form");
form.method="post";
form.action="login";
var usernmae = document.getElementById("username");
var pwd = document.getElementById("password");
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "text");
hiddenField.setAttribute("username",username.value);
hiddenField.setAttribute("password",pwd.value);
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
}
</script>
</head>
<body>
UserName:<input type="text" id="username" onblur="showpassword()"><br />
Password:<input type="password" id="password">
<input type="checkbox" name="remember" id="remember">
</input>Remeber Username<br />
<input value="Submit" type="button" onClick="remember()">
<input value="Delete" type="button" onClick="DelCookie()">
</body>
</html>
Upvotes: 0
Views: 271
Reputation: 12705
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "text");
hiddenField.setAttribute("username",username.value);
hiddenField.setAttribute("password",pwd.value);
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
sending parameters like this is not possible
sending data by form submission needs name attribute
on each of the html controls submitted
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "text");
hiddenField.setAttribute("name","username");
hiddenField.setAttribute("value",username.valuee);
form.appendChild(hiddenField);
var hiddenField2 = document.createElement("input");
hiddenField2.setAttribute("type", "text");
hiddenField2.setAttribute("name","password");
hiddenField2.setAttribute("value",pwd.value);
form.appendChild(hiddenField2);
document.body.appendChild(form);
form.submit();
Upvotes: 1
Reputation: 1428
You should use a <form>
tag in your HTML to do this, instead of creating a form programatically.
<body>
<form action="login" method="POST">
UserName:<input type="text" id="username" onblur="showpassword()"><br />
Password:<input type="password" id="password">
<input type="checkbox" name="remember" id="remember">
</input>Remeber Username<br />
<input value="Submit" type="button" onClick="remember()">
<input value="Delete" type="button" onClick="DelCookie()">
</form>
</body>
If you want to do this way (in JavaScript), you should create 2 input
with createElement instead of just one.
Upvotes: 0
Reputation: 12196
You're missing the most important thing in a form
element.. the form
it self.
How about wrapping the inputs in a big <form...>
clause.
Upvotes: 0