开发者

Javascript page to send credentials

JavaScript File

<html>
<head>
<script type="text/javascript">

function se开发者_如何学JAVAnd()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }




xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }

xmlhttp.open("POST","192.168.0.10/unp.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(encodeURI("Username="+document.form1.uname.value+"&password="+document.form1.pass.value));





}

</script>
</head>

<body>

<form name="form1" method="post">
USERNAME:<input type = "text" name="uname"><br>
PASSWORD:<input type = "password" name="pass">


<br>



<input type="submit" onclick="send()">
</form>
<div id="myDiv"><p>How are You </p></div>
</body>
</html>

PHP file running on the server. I want to store the credentials in a file on the server

<?php
$myfile="testfile.txt";
$fh=fopen($myfile,'a');
$stringdata=$_POST["Username"];
$stringdata1=$_POST["password"];
fwrite($fh,$stringdata);
fwrite($fh,$stringdata1);
fclose($fh);
?>
<html>
Details Submitted
</html>

The client page loads without error. The file textfile doest get created though. The response also is not returned. Is the Js file correct?


Were you expecting "Details submitted" to appear on the screen? From your code (which looks as the current document), it appears you think so but no, that is not the case. The text would be in xmlhttp.responseText and parsed out neatly in xmlhttp.responseXML.

EDIT

Vinod's comment pointed out some other errors in his code, in particular, his encoding was wrong (which probably was not the problem he was seeing) and the headers were missing (which probably was). He should have written:

var params = "Username="+encodeURIComponent(document.form1.uname.value)+
         "&password="+encodeURIComponent(document.form1.pass.value);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜