dongliao1860 2011-07-21 14:54
浏览 53
已采纳

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

I tested source code from this tutorial http://query7.com/php-jquery-todo-list-part-1 and just deploy their source taken here http://query7.com/wp-content/uploads/php-jquery-todolist.zip

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 ?

process.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>

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

    }

?>
  • 写回答

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题