weixin_33696106 2013-06-30 11:06 采纳率: 0%
浏览 82

将divs内容保存到数据库

I am using Aloha Editor and loving it.

I am stuck with saving data from different divs on the page to database.

I found a tutorial called contentEditable at: http://gazpo.com/2011/09/contenteditable/

It saves 1 div to database, however I have multiple divs on my web page and I can't work out the logics.

I would really appreciate if you could help?

I have the following php/Javascript/Ajax on my index.php:

 <?php
            //get data from database.
                include("db.php"); 
            ?>


<script>
    $(document).ready(function() {

        $("#save").click(function (e) {         
            var content = $('[class^="editable"]').html();  

            $.ajax({
                url: 'save.php',
                type: 'POST',
                data: {
                content: content
                },              
                success:function (data) {

                    if (data == '1')
                    {
                        $("#status")
                        .addClass("success")
                        .html("Data saved successfully")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   
                    }
                    else
                    {
                        $("#status")
                        .addClass("error")
                        .html("An error occured, the data could not be saved")
                        .fadeIn('fast')
                        .delay(3000)
                        .fadeOut('slow');   
                    }
                }
            });   

        });

        $('[class^="editable"]').click(function (e) {
            $("#save").show();
            e.stopPropagation();
        });

        $(document).click(function() {
            $("#save").hide();  
        });

    });

</script>

On my index.php, each divs with a unique class, for instance .editable1 and inside the div I have the following PHP to echo the data from database:

<div class="editable1">
<?php
        $sql = mysql_query("select text from content where element_id='1'");
        $row = mysql_fetch_array($sql);         
        echo $row['text'];
?>
</div>
<div class="editable2">
<?php
        $sql = mysql_query("select text from content where element_id='2'");
        $row = mysql_fetch_array($sql);         
        echo $row['text'];
?>
</div>

My save.php I have:

<?php
    include("db.php");
    $content = $_POST['content']; //get posted data
    $content = mysql_real_escape_string($content);  //escape string 

    $sql = "UPDATE content SET text = '$content' WHERE element_id = '1' ";
        $sql = "UPDATE content SET text = '$content' WHERE element_id = '2' ";


    if (mysql_query($sql))
    {
        echo 1;
    }

?>

My db.php:

<?php
    //database connection
    mysql_connect("localhost", "root", "root") or die(mysql_error());
    mysql_select_db("create") or die(mysql_error());
?>

The issue is that when I update text with Aloha editor in the div.editable1 it says "saved successfully" but when I refresh the page the divs are empty and database rows are also empty.

Any help would be very much appreciated :)

Thanks, Michel

  • 写回答

1条回答 默认 最新

  • 乱世@小熊 2013-06-30 12:19
    关注

    First you setup you javascript to pass the values of both div`s as content1 and content2

    var content1 = $('.editable1').html();  
    var content2 = $('.editable2').html();  
    
                $.ajax({
                    url: 'save.php',
                    type: 'POST',
                    data: {
                    content1: content1, content2: content2
                    },       
                    success: function(data) {
                       .......       
    

    Then on your php side you adjust the code to handle both input parameters.

    include("db.php");
        $content1 = $_POST['content1']; //get posted data
        $content1 = mysql_real_escape_string($content1);  //escape string 
    
        $content2 = $_POST['content2']; //get posted data
        $content2 = mysql_real_escape_string($content2);  //escape string 
    
    
         $sql = "UPDATE content SET text = '$content1' WHERE element_id = '1' ";
         mysql_query($sql);
    
            $sql = "UPDATE content SET text = '$content2' WHERE element_id = '2' ";
        if (mysql_query($sql))
        {
            echo 1;
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料