简体   繁体   English

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

[英]Inline CKEditor save to MySQL using AJAX/PHP

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? 目前,它仅使用jQuery,是否需要为此使用AJAX?

If so any tips would be great to point me in right direction as I'm not familiar that much with AJAX. 如果是这样的话,那么任何提示都可以使我朝正确的方向发展,因为我对AJAX不太熟悉。

Here is my code: 这是我的代码:

index.php 的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 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);
?>

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. 只是附带说明,“ echo”在js中不起作用。 You need to use 'alert()' or 'console.log()' 您需要使用“ alert()”或“ console.log()”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM