2011-07-21 14:54
浏览 52

为什么这个ajax jquery php做列表删除动作不起作用?

I tested source code from this tutorial and just deploy their source taken here

To my surprise after delete action, I refreshed the screen but item is still not deleted

enter image description here

I can't see any bug in code can you ?


    //Connect to the database
    $connection = mysql_connect('localhost:3316', 'root' , 'root');
    $selection = mysql_select_db('notes', $connection);

    //Was the form submitted?

    //Map the content that was sent by the form a variable. Not necessary but it keeps things tidy.
    $content = $_POST['content'];

    //Insert the content into database
    $ins = mysql_query("INSERT INTO `notes` (content) VALUES ('$content')");

    //Redirect the user back to the index page
    /*Doesn't matter if the form has been posted or not, show the latest posts*/

    //Find all the notes in the database and order them in a descending order (latest post first).
    $find = mysql_query("SELECT * FROM `notes` ORDER BY id DESC");

    //Setup the un-ordered list
    echo '<table border="0" cellpadding="5" cellspacing="0" class="list" width="100%">';

    //Continue looping through all of them
    while($row = mysql_fetch_array($find)){

    //For each one, echo a list item giving a link to the delete page with it's id.
    echo '<tr><td valign="middle" width="90%">' . $row['content'] . ' </td>
        <td valign="middle" width="10%"><form id="form" action="delete.php?id=' . $row['id'] . '" method="post">
        <input class="todo_id" name="todo_id" type="hidden" value="' . $row['id'] . '" />
        <input class="todo_content" name="todo_content" type="hidden" value="'  . $row['content'] . '" />
        <input type="image" src="images/delete.png" class="delete" name="delete" width="20px"  />


    //End the un-ordered list
    echo '</table>';
<script type="text/javascript">

        //Retrieve the contents of the textarea (the content)
        var todo_id = $(this).parent().find(".todo_id").val();
        var todo_content = $(this).parent().find(".todo_content").val();

        //Build the URL that we will send
        var url = 'submit=1&id=' + todo_id;

        //Use jQuery's ajax function to send it
           type: "POST",
           url: "delete.php",
           data: url,
           success: function(){

        //If successful , notify the user that it was added
           $("#msg").html("<p class='remove'>You just deleted: <i>" + todo_content + "</i></p>");

        //We return false so when the button is clicked, it doesn't follow the action
        return false;





    //Connect to the database
    $connection = mysql_connect('localhost', 'root' , '');
    $selection = mysql_select_db('notes', $connection);

    echo $id = $_POST['id'];
    //$id = $_GET['id'];

    //Delete the record of the post
    $delete = mysql_query("DELETE FROM `notes` WHERE `id` = '$id'");

    //Redirect the user


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongnai1876 2011-07-21 15:02

    process.php connects to a mysql database instance running on port 3316 with user root and password root, while delete.php connects to a mysql instance running on default port 3306 with user root and empty password: start from there

    打赏 评论
  • dongxiong2000 2011-07-21 15:02

    What happens if you put some $_GET variable in your browser and refresh again? http://localhost:8096/todolist?random=idunnosomethingiguess

    I suspect that this is a browser caching issue. You can completely prevent this with

    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");

    Of course, if you want to allow caching, then you can just use FF + Web Development toolkit and prevent caching that way.

    打赏 评论

相关推荐 更多相似问题