我已经成功创建了一个<textarea>
,可以将内容发布到数据库,而无需刷新按钮或刷新页面。但是,出于设计目的,我想使用可编辑的<div>
。因此,我使用了contenteditable="true"
。但是,ajax发布不再适用于<div>
。
我的代码:
$(document).ready(function() {
var timer;
$('#about').on('input', function() {
var value = this.value;
clearTimeout(timer);
timer = setTimeout(function() {
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);
}
});
}, 2000);
});
});
/* 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);
?>
文本区域
<div id="about" type="text" name="about" class="content livecomments" contenteditable="true" onpaste="OnPaste_StripFormatting(this, event);"><?php echo $edit ? ( empty( htmlentities ($row[ 'about' ]) ) ) ? 'test' : htmlentities ($row[ 'about' ]) : '';?></div>