3

I have three pages :

1)index.php (get results from select.php and put them into div#result )

2)select.php (loops into MySQL's table)

3)delete.php (gets user_id as a parameter and deletes it from MySQL's table).

My goal is : After user clicks on delete! to show updated results (after changes/deletes)

from MySQL's table

my problem is I could not know how to tell jQuery: listen process delete.php?id=123 & then

reload select.php while staying in index.php without redirecting to delete.php

so user actually does not see what happens or does not see that he's being redirected to

another page.

index.php

<html>
    <title>a</title>
    <head>
        <script type="text/javascript" src="jquery-1.8.0.js"></script>
        <script type="text/javascript">
            $(function() {
                $('#result').load('select.php');
            });
        </script>
    </head>
    <body>
        <div id="result"></div>
    </body>
</html>

select.php

<?php
    $con = mysql_connect("localhost","root","123");
    if (!$con) { die('Could not connect: '); }
    mysql_select_db("test", $con);

    $result = mysql_query("select * from users");   

    while($rs3 = mysql_fetch_array($result)) {
        echo $rs3["user_email"]." ".$rs3["user_name"]." ";
        echo "<a href=delete.php?id=".$rs3[user_id].">Delete</a>";
        echo "<br />";
    }
?>

delete.php

<?php
    $con = mysql_connect("localhost","root","123");
    if (!$con) { die('Could not connect: '); }
    mysql_select_db("test", $con);

    $id = mysql_escape_string($_GET["id"]);
    $delete = "delete from users where user_id='{$id}'";
    @mysql_query($delete);
?>

thank you.

4

1 回答 1

5

创建的不是链接,而是用于删除的 AJAX 查询。让 JQuery 调用链接。例如:

<script type="text/javascript">
$(function() {

    function refreshContent(){
        $('#result').load('select.php');    
    }

    $('#result').on('click','a',function(event){
        // prevent going by link `href`
        event.preventDefault();

        // get deleting row id
        var ids = $(this).data('ids');

        // make AJAX call for delete
        $.get("delete.php", { id: ids},function(data){
            // on success - refresh tcontent
            refreshContent();
        });

        return false;
    });

    // making content load on start
    $(document).ready(function(){
        refreshContent();
    });

});
</script>

你也必须添加ids<a>在这一行:

echo "<a href='delete.php?id=".$rs3[user_id]."' data-ids='".$rs3[user_id]."'>Delete</a>";
于 2012-09-12T09:08:54.163 回答