简体   繁体   English

ajax异步删除帖子-php

[英]ajax delete post asynchronously - php

I'm trying to create a add/delete/edit post system using php for a website. 我正在尝试使用php为网站创建添加/删除/编辑帖子系统。 I have the add working, so when the user enters in information it gets added to the database and then asynchronously gets added onto the page using ajax. 我有添加工作,因此,当用户输入信息时,它将添加到数据库中,然后使用ajax异步添加到页面上。 I want a similar function that deletes asynchronously as well. 我想要一个类似的功能,该功能也可以异步删除。 Right now, when I click delete, only the oldest post gets deleted AFTER refreshing the page. 现在,当我单击“删除”时,只有最旧的帖子在刷新页面后才会被删除。 It does not delete the post as soon as I click the delete button which is my goal. 单击我的目标是删除按钮后,它不会立即删除帖子。 This is what I have so far. 到目前为止,这就是我所拥有的。 The home.php is where my form is that collects the information and also prints out the information from the database. home.php是我的表单所在的位置,该表单收集信息并从数据库中打印出信息。 handledelete.php is where the deleting is handled. handleelete.php是处理删除的地方。

home.php home.php

<script>
    $(function() {
        $('#deleteButton').click(function(event) {
            event.preventDefault();

            $.ajax({
                type: "GET",
                url: "handle_delete.php",
                data : { entry_id : $(this).attr('data-id') },
                beforeSend: function(){
                }
                , complete: function(){
                }
                , success: function(html){
                    $("#show_entries").append(html);
                }
            });
        });
    });

</script>

<div id="entry">
    <form method="GET" action="handle_insert.php">
        <table align="center" width="30%" border="0">
            <tr>
                <td><input type="text" name="activity" id="activity" placeholder="Activity" required /></td>
            </tr>
            <tr>
                <td><input type="text" name="duration" id="duration" placeholder="Duration (hours)" required /></td>
            </tr>
            <tr>
                <td><input type="text" name="date" id="date_" placeholder="Date (YYYY-MM-DD)" required /></td>
            </tr>
            <tr>
                <td><button type="submit" name="submitButton" id="submitButton">Add input</button></td>
            </tr>

        </table>
    </form>
</div>

<!-- shows the previous entries and adds on new entries-->

<div id="show_entries">
    <?php
        $userID = $_SESSION["user"];
        $link = mysqli_connect('localhost', 'oviya', '', 'userAccounts');
        $query="SELECT * FROM dataTable WHERE user_id='$userID'";
        $results = mysqli_query($link,$query);

        while ($row = mysqli_fetch_assoc($results)) {
            echo '<div class="output" >';
                $entry_id = $row["entry_id"];
                $output= $row["activity"];
                echo "Activity: ";
                echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";
                $output= $row["duration"];
                echo "Duration: ";
                echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')." hrs"."<br>"."<br>";
                $output= $row["date_"];
                echo "Date: ";
                echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8')."<br>"."<br>";

                echo '<button type="submit" name="deleteButton" data-id='.$entry_id.' id= "deleteButton">Delete</button>';
               //echo '<button type="submit" name="editButton" data-id='.$entry_id.' id="editButton">Edit</button>';
            echo '</div>';
        }

    ?>
</div>

handle_delete.php handle_delete.php

session_start();
$user = 'oviya';
$password = '';
$db = 'userAccounts';
$host = 'localhost';
$port = 3306;

$link = mysqli_connect($host, $user, $password, $db);
mysqli_query($link,"GRANT ALL ON comment_schema TO 'oviya'@'localhost'");

if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

if(!empty($_GET["entry_id"])){
    $entry_id = mysqli_real_escape_string($link, $_GET["entry_id"]);

    $sql = "DELETE FROM dataTable WHERE entry_id = '$entry_id'";
    $result = mysqli_query($link, $sql);

    die();
    mysqli_close($link);

}

This line is the problem. 这条线是问题所在。 It would add elements if your AJAX call were returning HTML, which it isn't: 如果您的AJAX调用返回HTML,它将添加元素,但不是:

$("#show_entries").append(html);

Instead, you want to remove the deleted element, which you can reference directly and remove from the DOM: 相反,您要删除已删除的元素,可以直接引用该元素并从DOM中remove它:

$('#deleteButton').click(function(event) {
    event.preventDefault();
    // Get a reference to the whole row element.
    var row = $(this).parent();

    $.ajax({
        type: "GET",
        url: "handle_delete.php",
        data : { entry_id : $(this).attr('data-id') },
        success: function(html){
            // Remove the row
            row.remove();
        }
    });
});

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

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