文章来源:http://www.imtr.cn/html/n266.html
首先引入jquery文件:
<script src="./jquery.js"></script>
然后给input增加 data-id 属性,如下:
<input type='text' data-id='12' value='' autocomplete='off' >
autocomplete='off' 关闭表单的自动填写
添加js代码:
<script> //input内容改变时触发 $("input[data-id]").on("input", function() { const inputvalue = $(this).val(); const inputid = $(this).data("id"); // 获取 data-id 属性的值 $.ajax({// 发起 AJAX 请求 type: "POST", // 或 "GET",根据需要选择 url: "index.php", // 指向处理请求的 PHP 文件 data: { id: inputid, value: inputvalue }, // 发送到服务器的数据 success: function(response) { //alert("修改成功"); } }); }); </script>
index.php代码:
<?php //如果页面是以POST方式打开才能执行写入数据库 if($_SERVER['REQUEST_METHOD'] == "POST"){ $inputid = $_POST['id']; $inputvalue = $_POST['value']; $sql = "update imtr_admin set bz = '$inputvalue' where id = '$inputid'"; mysqli_query($con, $sql); exit(); } ?>
原文地址:http://www.imtr.cn/html/n266.html