簡體   English   中英

Ajax更新textarea,無需按鈕或頁面刷新

[英]Ajax update textarea without button or page refresh

我正在嘗試創建一個自動更新數據庫的文本區域,而無需按按鈕或刷新頁面。 發生“鍵控”后,將有一個計時器將倒計時2秒鍾。 如果在這2秒鍾內未進行任何其他輸入,則應在數據庫中更新數據。 如果輸入,計時器將重啟。

我已經在下面編寫了ajax代碼,它似乎不起作用。

 $(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); } }); }); 
 /* 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); ?> 

將ajax移到超時中,而不是執行提交,因為要使用ajax更新。

 $(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); }); }); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM