Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编辑单行?_Javascript_Php_Ajax - Fatal编程技术网

Javascript 如何编辑单行?

Javascript 如何编辑单行?,javascript,php,ajax,Javascript,Php,Ajax,因此,我目前正在一个论坛上工作,我希望能够让用户编辑他们自己的问题和答复。目前,我可以编辑问题排序*其他错误如下刚刚好。但是回复是我现在最大的问题。当前,它在回复下显示一个编辑按钮,但随后它将允许我编辑页面上的所有其他回复,然后当我保存它时,它将保存最后一行 <h3>Replies:</h3> <div id="replies"> <?php while($rows = mysqli_fetch_array($result2)) { ?>

因此,我目前正在一个论坛上工作,我希望能够让用户编辑他们自己的问题和答复。目前,我可以编辑问题排序*其他错误如下刚刚好。但是回复是我现在最大的问题。当前,它在回复下显示一个编辑按钮,但随后它将允许我编辑页面上的所有其他回复,然后当我保存它时,它将保存最后一行

<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);
?>
最初我在divs中有所有可编辑的内容,但由于某种原因,它使一些页面加载变得有趣,所以现在我希望使用p标记

我如何使它只有一个回复是可编辑的,并且只将该信息发送到editreply.php脚本

*我的另一个问题是一个次要问题,当我去编辑一些有链接的东西时,我的数据库里会出现大量的胡言乱语。例如,一位用户发布了LMAO:,我的数据库中的信息显示:


LMAO:;rel=nofollow>刚刚找到第一个问题的答案,我所做的只是将脚本移到if$editReply=true语句中,它就成功了

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