用于发送凭据的 Javascript 页面

发布于 2024-12-01 19:37:45 字数 1540 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫卆 2024-12-08 19:37:45

您是否希望屏幕上出现“详细信息已提交”??从您的代码(看起来像当前文档)来看,您似乎是这么认为的,但事实并非如此。文本将位于 xmlhttp.responseText 中,并在 xmlhttp.responseXML 中整齐地解析。

编辑

Vinod 的评论指出了他的代码中的一些其他错误,特别是他的编码错误(这可能不是他看到的问题)并且标头丢失(这可能是)。他应该这样写:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文