首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不需要按钮或页面刷新的Ajax更新文本区域

问不需要按钮或页面刷新的Ajax更新文本区域
EN

Stack Overflow用户
提问于 2019-03-09 03:12:29
回答 1查看 170关注 0票数 0

我正在尝试创建一个文本区,无需按下按钮或刷新页面即可自动更新数据库。在"keyup“发生后,将有一个计时器倒计时2秒。如果在这2秒内没有其他输入,那么应该在数据库中更新数据。如果进行了输入,定时器将重新启动。

我已经写了下面的ajax代码,它似乎不能工作。

​

代码语言:javascript
复制
$(document).ready(function() {
  var timer;
  $('#about').on('keyup', function() {
    var value = this.value;
    clearTimeout(timer);
    timer = setTimeout(function() {
      //do your submit here
      $("#about").submit()
      alert(value);
    }, 2000);
  });
  var about = $('#about').val().trim();
  $.ajax({
    url: "comment.php?customer_id=" + "<?php echo $customer_id; ?>",
    type: 'post',
    data: {
      about: about
    },
    success: function(response) {
      $('#cid').val(response);
    }
  });
});
代码语言:javascript
复制
/* This is comment.php */

<?php
session_start();
require_once 'config/config.php';
require_once 'includes/auth_validate.php';

$cid = $_GET['customer_id'];
$about = $_POST['about'];

$sql = ("UPDATE patient_id SET about = ? WHERE id = ?;");

$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "si", $about, $cid);
mysqli_stmt_execute($stmt);
?>

​

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-09 03:26:26

将ajax移至超时,而不是执行提交,因为您希望使用ajax进行更新。

​

代码语言:javascript
复制
$(document).ready(function() {
  var timer;
  $('#about').on('input', function() {
    var value = this.value;
    clearTimeout(timer);
    timer = setTimeout(function() {
      alert(value);
      $.ajax({
        url: "comment.php?customer_id=" + "<?php echo $customer_id; ?>",
        type: 'post',
        data: {
          about: value
        },
        success: function(response) {
          $('#cid').val(response);
        }
      });
    }, 2000);
  });
});

​

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55069559

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档