douwen1006 2017-03-30 09:47
浏览 56
已采纳

使用AJAX / PHP将内联CKEditor保存到MySQL

I have a few caption boxes that I want to be able to edit inline and to save these to my database to update a certain record in my table.

For some reason, nothing happens when I click the save button.. not even in the console.

It's just using jQuery at the moment, will I have to use AJAX for this?

If so any tips would be great to point me in right direction as I'm not familiar that much with AJAX.

Here is my code:

index.php

          <div class="caption" id="caption1" contenteditable="true" style="min-height: 450px;">    
           <?php
            $query3 = "SELECT * From (select * from ckeditor ORDER BY id DESC LIMIT 2) AS name ORDER BY id LIMIT 1";
            $show = mysql_query($query3, $con);
            while ($row = mysql_fetch_array($show))
            {
              echo $row['file'];
            }
            ?>
        </div>
        <button type="button" id="save"><span>Save</span></button>
            <script>
               $(document).ready(function (e) {

                  $("#save").click(function (e) {
                      var data = CKEDITOR.instances.caption1.getData();
                      var options = {  
                           url: "save.php",
                          type: "post",
                          data: { "editor" : encodeUriComponent(data) },
                         success: function (e) {
                           echo "Succesfully updated!";
                         }
                       };
                  }
               });
            </script>
      </div>

save.php

     <?php
$connection = mysql_connect("localhost", "", ""); 
$db = mysql_select_db("castle", $connection); 
//Fetching Values from URL
$data = nl2br($_POST['caption1']);
//Insert query
$query ="INSERT INTO `ckeditor`(`file`) VALUES ('$data')";
echo "Form Submitted Succesfully";
mysql_close($connection);
?>
  • 写回答

1条回答 默认 最新

  • doulianglou0898 2017-03-30 09:54
    关注

    You need to send the data to the server like this;

    $.ajax({
       url: "save.php",
       data: {
          "editor" : encodeUriComponent(data)
       },
       error: function() {
          //Error
       },
       success: function(data) {
          //Success
       },
       type: 'POST'
    });
    

    Currently you are just creating an object called 'options'

    Your code should look like this;

    $("#save").click(function (e) {
      var data = CKEDITOR.instances.caption1.getData();
      $.ajax({
       url: "save.php",
       data: {
           "editor" : encodeUriComponent(data)
       },
       error: function() {
          //Error
       },
       success: function(data) {
          alert('Success');
       },
       type: 'POST'
    });
    

    }

    Just a side note, 'echo' doesn't work in js. You need to use 'alert()' or 'console.log()'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥30 itest不允许查看成绩怎么办
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏