这里在我的图书数据库中,我为书桌的每一行都包括删除和编辑按钮。当我点击删除按钮时,它不会去deleteajax.php,即不要删除书的行。这里我创建了2个文件,一个用于books.php,另一个用于deleteajax.php.In 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>
deletebookajax.php
<?php
if(isset($_GET['book_id']))
{
$bookid = $_GET['book_id'];
}
include('assets/page_header.php');
?>
<input type="hidden" name="bookid" value=<?php if(isset($bookid)) echo $bookid; ?>>
<script>
$(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();
});
});
</script>
<?php
include('db.php');
//if(isset($_POST['bookid']))
//
//$bookid=mysql_real_escape_string($_POST['bookid']);
$delete = "delete from books WHERE bookid=$bookid";
$query1=mysql_query($delete);
if($query1)
{
$q=mysql_query("select * from books");
$display="<table>";
$display.="<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($row=mysql_fetch_array($q))
{
$display.="<tr>";
$display.= "<td>".$row['bookid']."</td>";
$display.= "<td>".$row['title']."</td>";
$display.= "<td>".$row['author']."</td>";
$display.= "<td>".$row['publisher']."</td>";
$display.="<td>".$row['numcopies']."</td>";
$display.="<td>".$row['shelfno']."</td>";
$display.="<td>".$row['status']."</td>";
$display.= "</tr>";
}
$display.="</table>";
}
else
{
$display.= "U can't delete The book";
//echo "nothing";
}
echo $display;
?>
</body>
</html>
答案 0 :(得分:0)
您不需要在文档就绪函数中定义函数,也不会被调用。您可以在全局范围内定义它,也可以直接调用.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();
});
});
答案 1 :(得分:0)
在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);
}
});
}