繁体   English   中英

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

[英]Why this ajax jquery php to do list delete action doesn't work?

我测试了本教程http://query7.com/php-jquery-todo-list-part-1的源代码,并在此处部署他们的源代码http://query7.com/wp-content/uploads/php-jquery -todolist.zip

令我惊讶的是,删除操作后,我刷新了屏幕,但项目仍未删除

在此处输入图像描述

我看不到代码中的任何错误,可以吗?

过程.php

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

    //Was the form submitted?
    if($_POST['submit']){

    //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
    header("Location:index.php");
    }
    /*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"  />

        </form>
        </td></tr>';
    }

    //End the un-ordered list
    echo '</table>';
?>
<script type="text/javascript">
    $(".delete").click(function(){

        //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
         $.ajax({
           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>");
           $("#content").val('');
           todolist();
           }
         });

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

    });

</script>

删除.php

<?php

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

    //delete.php?id=IdOfPost
    if($_POST['submit']){
    //if($_GET['id']){
    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
    header("Location:index.php");

    }

?>

如果您在浏览器中放入一些 $_GET 变量并再次刷新会发生什么? http://localhost:8096/todolist?random=idunnosomethingiguess

我怀疑这是浏览器缓存问题。 你可以完全防止这种情况

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");

当然,如果你想允许缓存,那么你可以使用 FF + Web 开发工具包并防止缓存。

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

暂无
暂无

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

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