用于发送凭据的 Javascript 页面
JavaScript 文件
<html>
<head>
<script type="text/javascript">
function send()
{
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 文件。我想将凭据存储在服务器上的文件中
<?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>
客户端页面加载时没有错误。但文件 textfile 并未创建。 响应也不会返回。 Js文件正确吗?
JavaScript File
<html>
<head>
<script type="text/javascript">
function send()
{
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否希望屏幕上出现“详细信息已提交”??从您的代码(看起来像当前文档)来看,您似乎是这么认为的,但事实并非如此。文本将位于
xmlhttp.responseText
中,并在xmlhttp.responseXML
中整齐地解析。编辑
Vinod 的评论指出了他的代码中的一些其他错误,特别是他的编码错误(这可能不是他看到的问题)并且标头丢失(这可能是)。他应该这样写:
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 inxmlhttp.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: