php - 为什么我的动态删除不起作用?

标签 php jquery mysql ajax

插入和动态表创建工作正常。一切都按照预期进行,通过按下该行上的按钮动态地从数据库中删除记录。尝试了多种不同的方法来访问该值,但没有结果。只是按一下按钮,什么也没有发生。任何帮助将不胜感激! (所有四个部分的代码如下。)

主页:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>PHP Test</title>
<link href="styleTest.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="container">
<header id="header"><strong>PHP Test</strong></header>
<div id="mainContent" align="center">
<form id="myForm" action="insert.php" method="post">
<table>
    <tr valign="baseline">
        <td>Name:</td>
        <td> <input id="name" type="text" name="name"/></td>
    </tr>
    <tr valign="baseline">
    <td>Comment:</td>
        <td> <input id="comment" type="text" name="comment"/></td>
    </tr>
    <tr valign="baseline">
        <td><input name="sub" id="sub" type="button" value="Save"></td>
    </tr>
</table>
</form>
<span id="result"></span>
</div><!-- mainContent -->
</div><!-- Container -->
<script src="jquery-1.8.1.min.js" type="text/javascript"></script>
<script src="my_script.js" type="text/javascript"></script>
</body>
</html>

插入页面:

<?php
//get the values passed by post
$name = mysql_real_escape_string($_POST['name']);
$comment = mysql_real_escape_string($_POST['comment']);

//create connection to Database
$hostname = "localhost";
$database = "test";
$username = "root";
$password = "";
$con = mysqli_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysqli_select_db($con,$database);

//Run the insert query and check to see if it processes
if(mysqli_query($con,"INSERT INTO info VALUES('','$name', '$comment')"))
{
  echo "Successfully Inserted";
  $sql2="SELECT * FROM info ORDER BY ID ASC";
  $result = mysqli_query($con,$sql2);
  echo "<table border='1'>
  <tr>
  <th>Name</th>
  <th>Comment</th>
  <th>Remove Entry</th>
  </tr>";

  while($row = mysqli_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>" . $row['NAME'] . "</td>";
    echo "<td>" . $row['COMMENTS'] . "</td>";
    echo "<td> <form id='myForm2' method='post' action='delete.php'><input name='del' id='del'
     type='button' value='Delete'><input id='id' type='hidden' name='id' value=".$row['ID']."></form></td>";
    echo "</tr>";
  }
  echo "</table>";
}
else
  echo "Save Failed!";
//close connection to database
mysqli_close($con);
?>

删除页面:

<?php
//create connection to Database
$hostname = "localhost";
$database = "test";
$username = "root";
$password = "";
$con = mysqli_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
mysqli_select_db($con,$database);
//retrieve posted variables
$id= mysql_real_escape_string($_POST['id']);

$sql="DELETE FROM info WHERE ID = $id";
$query = mysql_query($con,$sql);
if(mysql_affected_rows()>0){ 
$sql2="SELECT * FROM info ORDER BY ID ASC";
  $result = mysqli_query($con,$sql2);
  echo "<table border='1'>
  <tr>
  <th>Name</th>
  <th>Comment</th>
  <th>Remove Entry</th>
  </tr>";

  while($row = mysqli_fetch_array($result))
  {
    echo "<tr>";
    echo "<td>" . $row['NAME'] . "</td>";
    echo "<td>" . $row['COMMENTS'] . "</td>";
    echo "<td> <form id='myForm2 method='post' action='delete.php'><input name='del' id='del' type='button' value='Delete'><input id='id' type='hidden' name='id' value=".$row['ID']."></form></td>";
    echo "</tr>";
  }
  echo "</table>";
}else{
echo "Error deleting Data";
}
mysqli_close($con);
?>

脚本:

$(document).ready(function()
{
  $("#sub").click( function()
   {
        //Check to see that no fields are left blank if one is all fields are cleared and save is failed from insert.php.
        if ($("#name").val()=="")
        {
            clearInput();
            return false;
        }
        else if($("#comment").val()=="")
        {
            clearInput();
            return false;
        }
        //save the fields into an array and post to the result span then clear inputs   
        $.post( $("#myForm").attr("action"), 
        $("#myForm :input").serializeArray(), 
        function(info){ $("#result").html(info);});
        clearInput();  
  });

  $('#del').click(function()
  {
       $.post( "delete.php",$("#myForm2 :input").serialize(),function(info){ $("#result").html(info);});
  });
});

$("#myForm").submit( function() {
  return false; 
});

最佳答案

您有多个具有相同 ID 的元素 del ,由于输入是在循环内创建的 - 当您使用 id 选择器时,它将仅返回具有给定 id 的第一个元素,因此您的单击处理程序将仅注册到第一个元素。您可以使用 class 属性代替 id 对相似的元素进行分组。

更改<input name='del' id='del' type='button' value='Delete'>

<input name='del' class='del' type='button' value='Delete'>

然后使用类选择器来注册处理程序。

$('#del').click(function () {
    $.post("delete.php", $("#myForm2 :input").serialize(), function (info) {
        $("#result").html(info);
    });
});

关于php - 为什么我的动态删除不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462297/

相关文章:

jquery - Fancybox:想要使用与/anchor 不同的元素

php - 如何查看准备好的语句的内容?

php - 需要帮忙。如何在使用 laravel 中的用户详细信息登录后创建 session ?

php允许内存大小在图像上传旋转时耗尽

PHP+MYSQL+php调用程序

php mysql 搜索,突出显示包含提供的关键字的部分结果

jquery - 页面刷新在 Firefox 中不起作用

javascript - 通过 jQuery 更改动态创建元素的 CSS

php - Docker php :5. 6-Apache 开发环境缺少卷装载权限

Php 和 htaccess 错误 - "no input file specified"