dongmo2324 2015-07-02 07:29
浏览 42
已采纳

使用Ajax / jQuery(内联文本编辑)在<div>标记内检索数据

I've been working on this problem for a few hours, but there is some mistake somewhere in the javascript file (I believe), but I can't figure it out.

Right now the alert(msg) gives me an Undefined index: headline/text in editPost.php.

The following PHP code is in a file profile.php. I want to retrieve the data within the <div>I want this data</div> tags (i.e. I want to retrieve the data in $row['headline'] and $row['text'].

while ($row = mysqli_fetch_array ($resultPost, MYSQLI_ASSOC)) {
    echo '<h1><div contenteditable="true" data-headline="headline" data-id=' . $row['id'] . '>' . $row['headline'] . '</div></h1>';
    echo '<p><div contenteditable="true" data-text="text" data-id=' . $row['id'] . '>' . $row['text'] . '</div></p>';
}

This is how I try to retrieve the data (seperate .js file):

$(document).ready(function() {
$('body').on('blur', "div[contenteditable=true]", function() {

    var headline = $("div[data-name='headline']:visible").text();
    var text = $("div[data-name='text']:visible").text();
    $.ajax({
        type: 'POST',
        url: 'editPost.php',
        data: {
            content: $.trim($(this).text()),
            id: $(this).data('id'),
            headline: $(this).data(headline),
            text: $(this).data(text),
        },
        success: function(msg) {
            alert(msg);
        }
    });
});
});

The function above then posts the data to editPost.php, which submits the data to a database. Below is a snippet of how I do that:

$headline = $_POST['headline'];
$text = $_POST['text'];
$id = $_POST['id'];
$sql = "UPDATE blogpost SET headline = '$headline', text = '$text', edit_time = NOW(6) WHERE id = '$id'";

In the current state, when the data is sent to the database, it finds the correct table (using the id), and inserts "" in both the headline and text fields, but it updates the edit_time correctly.

Thank you!

  • 写回答

1条回答 默认 最新

  • douchengchen7959 2015-07-02 12:28
    关注

    I took a break for a few hours, and came back with more thoughts on how to solve it. After a little tweaking here and there, I finally did it.

    For those of you who visit this thread at a later time, this is what I changed in order for it to work:

    My profile.php snippet is now like this (I switched data-headline="headline" to name="headline" etc.):

    while ($row = mysqli_fetch_array ($resultPost, MYSQLI_ASSOC)) {
        echo '<h1><div contenteditable="true" name="headline" data-id=' . $row['id'] . '>' . $row['headline'] . '</div></h1>';
        echo '<p><div contenteditable="true" name="text" data-id=' . $row['id'] . '>' . $row['text'] . '</div></p>';
    }
    

    My javascript file now consists of two functions with minor differences (one for each field). Yes, I'm certain there is a better way to solve this:

    $(document).ready(function() {
    $('body').on('blur', "div[name=headline]", function() {
    
        var headline = $("div[name='headline']:visible").text();
        $.ajax({
            type: 'POST',
            url: 'editPost.php',
            data: {
                headlineContent: $.trim($(this).text()),
                id: $(this).data('id'),
                headline: $(this).data(headline),
            },
            success: function(msg) {
                alert(headline);
            }
        });
    });
    });
    
    $(document).ready(function() {
    $('body').on('blur', "div[name=text]", function() {
    
        var text = $("div[name='text']:visible").text();
        $.ajax({
            type: 'POST',
            url: 'editPost.php',
            data: {
                textContent: $.trim($(this).text()),
                id: $(this).data('id'),
                text: $(this).data(text),
            },
            success: function(msg) {
                alert(text);
            }
        });
    });
    });
    

    I changed how the elements were targeted, so targeting one element wouldn't duplicate the content over to the other element.

    Finally, in my editPost.php file, I added a check to see whether or not a variable is empty. If it is empty, that means the element didn't get updated, hence why it only updates the other element.

    $headline = $_POST['headlineContent'];
    $text = $_POST['textContent'];
    $id = $_POST['id'];
    if (!empty($headline)) {
        $sql = "UPDATE blogpost SET headline = '$headline', edit_time = NOW(6) WHERE id = '$id'";
    } elseif (!empty($text)) {
        $sql = "UPDATE blogpost SET text = '$text', edit_time = NOW(6) WHERE id = '$id'";
    }
    

    As you can see, the code itself is far from perfect (pretty horrible actually), but it works for now. I'll definitely try to improve on it in the future, but any feedback would be appreciated (I am aware this is not the place for codereview).

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

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题