Ajax发布不适用于Contenteditable

时间:2019-03-09 17:59:50

标签: javascript php html ajax

我已经成功创建了一个<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>

1 个答案:

答案 0 :(得分:1)

问题在这里

var about = $('#about').val().trim();

<div>元素没有value。您应该使用html()text()

var about = $('#about').text().trim();