I have a HTML file that calls a CGI script. When I click logout button, POST method sends all the fields. Is this a known bug?
Here is my Javascript code:
var request = null, session = null;
function postEscape(val) {
return encodeURIComponent(val).replace(/%20/g, "+")
}
function submit_logout() {
var request=null
if( !document.form.loggedIn.checked ) return
logout = new XMLHttpRequest()
logout.open("POST", "client.cgi", true /* asynchronous? */ )
logout.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
console.log("action=logout&session=" + postEscape(document.form.session.value)+"&host="+postEscape(document.form.host.value)+"&port="+postEscape(document.form.port.value))
logout.send("action=logout&session=" + postEscape(document.form.session.value)+"&host="+postEscape(document.form.host.value)+"&port="+postEscape(document.form.port.value)+"&action=logout")
form.loggedIn.checked = false
}
This is what I see in POST data:
username=&password=&host=somehost.com&port=6700&action=logout&display=++++&session=
and here is my form
<body>
<form name = "form">
<input name="username" size="25" type="text">username<br>
<input name="password" size="25" type="text">password<br>
<input name="host" size="25" type="text">host<br>
<input name="port" size="25" type="text">port<br>
<input name="login" value="Login" type="button" onclick="submit_login()"><input type="checkbox" name="loggedIn" id = "loggedIn" onclick="this.checked=!this.checked">
<input name="action" value="logout" type="submit" onClick="submit_logout()"><br>
<textarea name="display" rows="24" cols="68"> </textarea><br>
<input type="hidden" name="session" value="">
<input name="message" size="64" maxlength="64" type="text"><input name="send" value="send" type="button" onClick = "send()"><p></p>
</form>
Try changing the following: You are submitting everything becayse of the
type="submit"for the logout. It should be atype="button".<input type="button" />buttons will not submit a form – they don’t do anything by default. They’re generally used in conjunction with JavaScript as part of an AJAX application.<input type="submit" />buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.to