如何编辑单行?

问题描述 投票:0回答:1

所以我目前正在开发一个论坛,我希望能够让用户编辑自己的问题和回复。目前我能够编辑问题(其他错误如下),就好了。但回复是我现在最大的问题。目前它在回复下显示一个编辑按钮,但是它允许我编辑页面上的所有其他回复,然后当我保存它时,它会保存最后一行。

<h3>Replies:</h3>
<div id="replies">

<?php
while($rows = mysqli_fetch_array($result2)) {
?>
    <p id="dates"><?php echo $rows['a_datetime']; ?></p>
    <div id="reply">
    <b><p><?php echo $rows['a_username']; ?></p></b>
    <?php
    // The Regular Expression filter
    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

    // The Text you want to filter for urls
    $text = htmlspecialchars($rows['a_answer']);

    // Check if there is a url in the text
    if(preg_match($reg_exUrl, $text, $url)) {
        $url = preg_replace("/^http:/i", "https:", $url);

        // make the urls hyper links
        echo preg_replace($reg_exUrl, '<a title="Opening this link will take you to a new page" alt="External Link Deleted" target="_blank" href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', '<p id="reply'.$rows['a_id'].'">'.$text.'</p>');

    } else {
        ?>
        <p id="reply<?php echo $rows['a_id']; ?>"><?php echo htmlspecialchars($rows['a_answer']); ?></p>
        <?php
    }
    if($_SESSION['username'] == $rows['a_username']) {
        $editReply = true;
        ?>
        <div id="questionId"><?php echo $rows['question_id'];?></div>
        <div id="replyId"><?php echo $rows['a_id'];?></div>

        <button id="editReply">Edit</button>
        <button id="saveReply">Save</button>
        <button id="cancelReply">Cancel</button>

        <script type="text/javascript">
            $(document).ready(function(argument) {
                $('#saveReply').hide();
                $('#cancelReply').hide();
            });
        </script>
        <?php
    }
    ?>
    </div>

    <script type="text/javascript">
        $(document).ready(function(argument) {
            $('#editReply').click(function() {
                $('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','true');
                $('#reply<?php echo $rows['a_id']; ?>').css('background','white');
                $('#reply<?php echo $rows['a_id']; ?>').css('border','solid 1px');
                $('#saveReply').show();
                $('#cancelReply').show();
                $('#editReply').hide();
            });

            $('#saveReply').click(function() {
                // Get edit field value
                $detail = $('#reply<?php echo $rows['a_id']; ?>').html();
                $q_id = $('#questionId').html();
                $a_id = $('#replyId').html();

                $.ajax({
                    url: 'editReply.php',
                    type: 'post',
                    data: {detail: $detail, q_id: $q_id, a_id: $a_id},
                    datatype: 'html',
                });

                $('#editReply').show();
                $('#saveReply').hide();
                $('#cancelReply').hide();
                $('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','false');
                $('#reply<?php echo $rows['a_id']; ?>').css('background','#D8D8D8');
                $('#reply<?php echo $rows['a_id']; ?>').css('border','none');
            });

            $('#cancelReply').click(function() {
                $('#editReply').show();
                $('#saveReply').hide();
                $('#cancelReply').hide();
                $('#reply<?php echo $rows['a_id']; ?>').attr('contenteditable','false');
                $('#reply<?php echo $rows['a_id']; ?>').css('background','#D8D8D8');
                $('#reply<?php echo $rows['a_id']; ?>').css('border','none');
            });
        });
    </script>
    <?php
}
?>

editreply.php:

<?php
$host = "host"; // Host name 
$user = "username"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "db"; // Database name 
$tbl_name = "fanswer"; // Table name 

// Connect to server and select databse.
$conn = mysqli_connect($host, $user, $password)or die("cannot connect"); 
mysqli_select_db($conn, $db_name)or die("cannot select DB");

$detail = htmlspecialchars($_POST['detail']);
$q_id = $_POST['q_id'];
$a_id = $_POST['a_id'];

// Add your validation and save data to database

echo $detail;
echo $q_id;
echo $a_id;

$sql = "UPDATE $tbl_name SET a_answer = '$detail' WHERE question_id='$q_id' AND a_id= '$a_id'";
$result = mysqli_query($conn, $sql);
?>

最初我有div中的所有可编辑内容,但由于某种原因,它使一些页面加载有趣,所以现在我希望使用p标签。

我怎样才能使它只有一个回复是可编辑的,所以它只将该信息发送到editreply.php脚本?

*我的另一个问题,这是一个副作用问题,当我去编辑其中有链接的东西时,我的数据库中出现了大量的乱码。例如。用户发布LMAO:https://afunnypic.com,我的数据库中的信息说:

LMAO:<a title="Opening此链接将带您进入新页面“alt="External Link Deleted" target="_blank" href="https://afunnypic.com&quot; rel="nofollow"> https://afunnypic.com</a><br&gt;

哪个我不明白。

javascript php ajax
1个回答
1
投票

刚刚找到我对第一个问题的答案,我所做的就是将脚本移到if($ editReply = true)语句中并且它有效!

如果有人可以帮助编辑链接的第二位,这将是伟大的!

© www.soinside.com 2019 - 2024. All rights reserved.