Php “删除”按钮不起作用

Php “删除”按钮不起作用,php,jquery,ajax,Php,Jquery,Ajax,在我的图书数据库中,我为图书表的每一行都包含了删除和编辑按钮。当我单击删除按钮时,它不会转到deleteajax.php,也就是说,它不会删除图书的这一行。在这里,我创建了两个文件,一个是books.php,另一个是deleteajax.php。在deletebookajax.php中,我包含了ajax代码和jquery脚本……你能吗请检查我在哪里遇到的错误。 books.php <?php include('assets/page_header.php'); ?> <?ph

在我的图书数据库中,我为图书表的每一行都包含了删除和编辑按钮。当我单击删除按钮时,它不会转到deleteajax.php,也就是说,它不会删除图书的这一行。在这里,我创建了两个文件,一个是books.php,另一个是deleteajax.php。在deletebookajax.php中,我包含了ajax代码和jquery脚本……你能吗请检查我在哪里遇到的错误。 books.php

<?php
include('assets/page_header.php');
?>

<?php
//error_reporting(0);
include('db/db.php');




$str="select * from books";

$query1=mysql_query($str);
echo($query1);
$q=mysql_num_rows($query1);

//$query2=mysql_query("select status from bookrentalinfo where bookid=$bookid");
//echo $query2;
//$res=mysql_fetch_array($query2);
echo "<table>";
echo "<tr><th>BookID</th><th>Title</th><th>Author</th><th>Publisher</th><th>numcopies</th><th>shelfno</th><th>status</th><th>Action</th></tr>";
while($rows=mysql_fetch_array($query1))
{

echo "<tr>";
echo "<td>".$rows['bookid']."</td>";
echo "<td>".$rows['title']."</td>";
echo "<td>".$rows['author']."</td>";
echo "<td>".$rows['publisher']."</td>";
echo "<td>".$rows['numcopies']."</td>";
echo "<td>".$rows['shelfno']."</td>";
echo "<td>".$rows['status']."</td>";
echo "<td><button class='button1' value='delete' name='delete' onclick='delete()'>delete</button></td>";
echo "<td><a href='edit1form.php?book_id=".$rows['bookid']."'>Edit</a></td>";


//echo "<td><button type='button'>delete</button></td>";
/*if($res['status']=="BORROWED")
{
echo "Sorry You Can't Delete The Book";
}
else
{
echo "The Row Is Deleted";
}*/


echo "</tr>";
}
echo "</table>";
?>
</body>
</html>


您不需要在documentready函数中定义函数,它永远不会被调用。您可以在全局范围内定义它,也可以直接调用
.ajax
方法:

$(document).ready(function(){

    $(".button1").click(function(e){

    var bookid = $("#bbookid").val();
    var title = $("#btitle").val();
    var author = $("#bauthor").val();
    var publisher = $("#bpublisher").val();
    var numcopies = $("#bnumcopies").val();
    var shelfno= $("#bshelfno").val();
    var status = $("#BooksStatus").val();
    var dataString='bbookid='+bookid+'&btitle='+title+'&bauthor='+author+'&bpublisher='+publisher+'&bnumcopies='+numcopies+'&bshelfno='+shelfno+'&BooksStatus='+status;
    if(author==''||title==''||publisher==''||numcopies==''||shelfno==''||status=='')
        {
        alert("Please Fill All Fields");
        }
        else
        {
        //function delete()
        //{
            // AJAX Code To Submit Form.
            $.ajax({
            type: "POST",
            url: "db/deletebookajax.php",
            data: dataString,
            cache: false,
            success: function(result){
            alert("submitted"+result);
            $('#display').html(result);
            },
            error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
            }
            });
        //}
        }
        e.preventDefault(); 
        });
        });

在click事件外声明函数,并将其称为以下内容:

if(author==''||title==''||publisher==''||numcopies==''||shelfno==''||status==''){
    alert("Please Fill All Fields");
} else {
    delete();    
}


function delete() {
// AJAX Code To Submit Form.
$.ajax({
    type: "POST",
    url: "db/deletebookajax.php",
    data: dataString,
    cache: false,
    success: function(result){
    alert("submitted"+result);
    $('#display').html(result);
    },
    error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
    }
});
}

使用事件委派。。查看为什么js函数
delete
在ajax文件中定义?
book.php
中的代码如何知道此函数?永远不要使用msql_*where is
.button1
?为什么要转到同一页?好的,那么您是否尝试将代码放入没有函数的地方,如@KAD的代码片段所示?当您单击按钮时,是否确定光标处于其他状态如果有可能的话。如果您确认一下就更好了。当我单击按钮时,它不会转到脚本,即使我使用alert(“hi”)来表示simplyPut此代码处于directly else状态。并检查是否出现警报错误?“$.ajax({type:“POST”,url:“db/deletebookajax.php”,数据:dataString,cache:false,成功:函数(结果){alert('success');},错误:函数(xhr,ajaxOptions,thrownError){alert('error');};””
if(author==''||title==''||publisher==''||numcopies==''||shelfno==''||status==''){
    alert("Please Fill All Fields");
} else {
    delete();    
}


function delete() {
// AJAX Code To Submit Form.
$.ajax({
    type: "POST",
    url: "db/deletebookajax.php",
    data: dataString,
    cache: false,
    success: function(result){
    alert("submitted"+result);
    $('#display').html(result);
    },
    error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
    }
});
}