【HTML页面使用Ajax+POST提交参数,PHP接收参数并保存为TXT】

8 篇文章 3 订阅

效果演示

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

源码

test_Post.html

<html>
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <script>
    function sendData() {
      var username = document.getElementById("username").value;
      var password = document.getElementById("password").value;
      if (window.XMLHttpRequest) {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码
        xmlhttp = new XMLHttpRequest();
      } else {
        //IE6, IE5 浏览器执行的代码
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
          document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
      };

      xmlhttp.open("POST", "test1.php", true);
      // GET方式:"test_Get.php?q=" + str
      xmlhttp.setRequestHeader(
        "Content-type",
        "application/x-www-form-urlencoded"
      );
      xmlhttp.send("username=" + username + "&password=" + password);
    }
  </script>
  <body>
    <p><b>在输入框中输入用户名和密码:</b></p>
    <form>
      用户名: <input type="text" name="username" id="username" />
      <br />
      密码:<input type="password" name="password" id="password" />
      <br />
      <button type="button" onclick="sendData()">提交</button>
    </form>
    <p>返回值: <span id="txtHint"></span></p>
  </body>
</html>

test_Post.php

<?php
//从POST中获取参数
$username = $_POST["username"];
$password = $_POST["password"];

$hint = file_put_contents("test.txt", $username . ":" . $password);

if ($hint > 0) {
    $response = "写入成功!用户名:" . $username . ",密码:" . $password;
} else {
    $response = "写入失败!";
}

//输出返回值
echo $response;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值