在DB

时间:2016-12-15 02:49:53

标签: javascript php mysql ajax summernote

我在使用ajax在DB中保存summernote值时出现问题,但是当我不使用ajax时,一切正常。

说清楚。

<?php
  include 'db.php';
  $db=new db;

  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
    if($_POST['act']=='save'){
      $content=$_POST['content'];
      $id=25;

      $query="UPDATE content SET content=? WHERE id=?";
      $db->execquery($query, 'si', array($content, $id));
      echo json_encode(array("response"=>'success'));
      exit();
    }
  }
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 
  <title>summernote</title>
  <!-- include jquery -->
  <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> 

  <!-- include libraries BS3 -->
  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css" />
  <script type="text/javascript" src="//netdna.bootstrapcdn.com/bootstrap/3.0.1/js/bootstrap.min.js"></script>

  <!-- include summernote -->
  <link rel="stylesheet" href="../dist/summernote.css">
  <script type="text/javascript" src="../dist/summernote.js"></script>

  <script type="text/javascript">
    $(function() {
      $('.summernote').summernote({
          toolbar: [
            // [groupName, [list of button]]
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']]
          ]
        });

      $('form').on('submit', function (e) {
        e.preventDefault();

        content=$('.summernote').summernote('code');
        data='act=save&content='+content;
        $.ajax({
          type: "POST",
          dataType: "JSON",
          data: data,
        })
        .fail(function(e){
            alert(e.response);
        })
        .done(function(data){
            if(data.response=='success'){
                alert('success!');
            }
        });
      });
    });
  </script>
</head>
<body>
<form action="#" novalidate>
  <div class="form-group">
    <label for="input">Text</label>
    <textarea class="form-input" id="input"></textarea>
  </div>
  <div class="form-group">
    <label for="contents">Contents</label>
    <textarea name="text" class="summernote" id="contents" title="Contents"></textarea>
  </div>
  <button type="submit" class="btn btn-default">submit</button>
</form>
</body>
</html>

无论如何,当我在summernote中输入简单文本时,值正在保存,但是当我从msword中粘贴一些文本时,会保存正在保存的值。

我尝试了一些东西,当我按下提交时,我也将summernote值放入额外的textarea然后当我在DB中手动粘贴值时,一切都很好。我也尝试复制codeview值,然后手动粘贴到DB中,然后它也很好。请帮忙。

提前致谢!

1 个答案:

答案 0 :(得分:3)

我遇到了同样的问题,我用以下方法解决了这个问题:

的Javascript

content = encodeURIComponent($('.summernote').summernote('code'));

PHP

urldecode($_POST[content])