AJAX A HREF的onclick让PHP变量和执行PHP文件变量、文件、HREF、AJAX

2023-09-10 18:29:22 作者:淡笑凡尘

在我问这个问题,我已经提到的this问题。但是,似乎不工作。如果不使用AJAX,我可以让我的帖子删除,但之后实现Ajax的deleteAtc.php似乎不工作。

Before I ask this question, I have already referred to the example of this question. However, it seems doesn't work. Without using ajax, I can get my post deleted but after implement ajax, the deleteAtc.php seems to be not working.

我删除页code(delete.php)

My delete page code (delete.php)

    <h4>Select an Article to Delete:</h4>

    <?php foreach ($articles as $article) { ?>

        <span><?php echo $article['article_title']; ?></span> <a href="#" id="link">Delete</a><br />

        <script type="text/javascript">
        $(function(){
            $('#link').click(function(){
                var id = <?php echo $article['article_id']; ?>;
                $.ajax({
                    type: "GET",
                    url: "deleteAtc.php",
                    data: "id"+id+,
                    sucess: function() {
                        $('#sucess').html(data);
                    }
                })
                return false;
            });
        });
        </script>
        <div id="success"></div><br />
    <?php } ?>

虽然我deleteAtc.php code:

While my deleteAtc.php code:

<?php 

session_start();

include_once('../includes/connection.php');

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();

        echo "Article deleted";
    }
}    
?>

我想要做的,是删除不重定向到 deleteAtc.php 的记录,它会删除了记录,并替换文章删除

What I'm trying to do here is to delete the record without redirect to deleteAtc.php, it will remove out the record and replace Article Deleted

我想知道我做错什么了阿贾克斯的一面呢?

请参阅下面的更新问题

,这里是我的更新code:

Based on the answer below, here is my updated code:

delete.php

delete.php

<h4>Select an Article to Delete:</h4>

<div id="message"></div>

<?php foreach ($articles as $article) { ?>

<span><?php echo $article['article_title']; ?></span> 
<a href="#" class="link" data-artid="<?php echo $article['article_id']; ?>">Delete</a><br />
<?php } ?>

剧本

<script type="text/javascript">
        $(function(){
            $('.link').click(function(){
                var elem = $(this);
                $.ajax({
                    type: "GET",
                    url: "deleteAtc.php",
                    data: "id="+elem.attr('data-artid'),
                    dataType:"json",  
                    success: function(data) {
                        if(data.success){
                               elem.hide();
                               $('#message').html(data.message);
                        }
                    }
                });
                return false;
            });
        });
        </script>

deleteAtc.php

deleteAtc.php

<?php 

session_start();

include_once('../includes/connection.php');

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();

        //Also try to handle false conditions or failure
        echo json_encode(array('success'=>TRUE,'message'=>"Article deleted"));
    }
}    
?>

不知为什么,如果我删除两家唱片的时间,只有第一个记录呼应的结果,第二个结果中删除不呼应出结果。

Somehow, if I delete two record at a time, only the first record echo the result, the second result deleted doesn't echo out the result.

想知道,可以添加jQuery的动画.show成功的消息,并.hide记录中删除?

Am wondering, possible to add jquery animation to .show the success message and .hide the record deleted?

推荐答案

首先标识都注定不会被复制,使用类选择器代替。你也可以使用自定义的数据属性来存储物品的ID。

First of all IDs are not meant to be duplicated, use class selector instead. Also you could make use of custom data attributes to store the id of the article.

尝试

<h4>Select an Article to Delete:</h4>

<div id="message"></div>

<?php foreach ($articles as $article) { ?>

<span><?php echo $article['article_title']; ?></span> 
<a href="#" class="link" data-artid="<?php echo $article['article_id']; ?>">Delete</a><br />
<?php } ?>

剧本

<script type="text/javascript">
        $(function(){
            $('.link').click(function(){
                var elem = $(this);
                $.ajax({
                    type: "GET",
                    url: "deleteAtc.php",
                    data: "id="+elem.attr('data-artid'),
                    dataType:"json",  
                    success: function(data) {
                        if(data.success){
                               elem.hide();
                               $('#message').html(data.message);
                        }
                    }
                });
                return false;
            });
        });
        </script>

而在服务器端

And in server side

<?php 

session_start();

include_once('../includes/connection.php');

if (isset($_SESSION['logged_in'])) {
    if (isset($_GET['id'])) {
        $id = $_GET['id'];

        $query = $pdo->prepare('DELETE FROM articles WHERE article_id = ?');
        $query->bindValue(1, $id);
        $query->execute();

        //Also try to handle false conditions or failure
        echo json_encode(array('success'=>TRUE,'message'=>"Article deleted"));
    }
}    
?>