2017-05-28 54 views
2

我想用JavaScript添加確認框。所以,這裏是我用過的代碼。當我點擊刪除按鈕時,它會顯示一條確認消息,當我單擊確定時,它不會刪除我的行。如何修復這個JavaScript函數?

有什麼不對呢?我怎樣才能解決這個問題?

a table

這裏是索引頁

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 

 
<html> 
 
<head> 
 
    <!-- Bootstrap core CSS --> 
 
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"> 
 
</head> 
 
<body> 
 
<div class="row"> <!-- Start Of The Row Class --> 
 
<div class="col-md-8 col-sm-4 hero-feature"> <!-- Start Of The Col Class --> 
 
<br><br> 
 
<?php 
 
error_reporting(E_ALL^E_DEPRECATED); 
 
include('ksdb.php'); 
 
// get results from database 
 
$result = mysql_query("SELECT * FROM academic") 
 
or die(mysql_error()); 
 
?> 
 
<table class='table table-bordered'> 
 
<tr class='danger'> 
 
<th>ID</th> 
 
<th>Name</th> 
 
<th>Username</th> 
 
</tr> 
 
<?php 
 
// display data in table 
 
while($row = mysql_fetch_array($result)) { 
 
\t ?> 
 
<tr> 
 
<td><?php echo $row['id']; ?></td> 
 
<td><?php echo $row['name']; ?></td> 
 
<td><?php echo $row['username']; ?></td> 
 
<td><a href="AdminChangePw.php?id=<?php echo $row['id']; ?>" alt="edit" >Change</a></td> 
 
<td><a href="AdminEdit.php?id=<?php echo $row['id']; ?>" alt="edit" >Edit</a></td> 
 
<td><input type="button" onClick="deleteme(<?php echo $row['id']; ?>)" name="Delete" value="Delete"></td> 
 
</tr> 
 
<script language="javascript"> 
 
function deleteme(delid) 
 
{ 
 
if(confirm("Do you want Delete!")){ 
 
window.location="AdminDel.php?id=' +id+'"; 
 
return true; 
 
} 
 
} 
 
</script> 
 
<?php 
 
} 
 
?> 
 
</table> 
 
</div> <!-- End Of The Col Class --> 
 
</div> <!-- End Of The Row Class --> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"></script> 
 
</body> 
 
</html>

+0

是它從數據庫中刪除? – AdhershMNair

+0

@AdhershMNair - 編號 –

回答

1

更改ID爲 'delid'。您正在將此功能稱爲deleteme(delid)。 所以你必須使用delid,而不是id。

function deleteme(delid) 
{ 
if(confirm("Do you want Delete!")){ 
window.location.href="AdminDel.php?id="+delid; /*Change id to 'delid'*/ 
return true; 
} 
} 
+0

我認爲不應該有'delid'的單引號。 OP正在使用'is_numeric($ _ GET ['id'])'驗證id,所以帶單引號的id不會被認爲是有效的。 –

+0

是的。謝謝。 – AdhershMNair

+0

@AdhershMNair和Abhijit_Parida - 它的100%完美工作.. 非常感謝! :d –

0

使用 window.location.href 代替了window.location的

+0

謝謝。它也可能對我有幫助。 – AdhershMNair

+0

但是,window.location.href和window.location之間有什麼不同? –

0

有幾個與你的函數問題:

function deleteme(delid) { 
    if(confirm("Do you want Delete!")){ 
     window.location="AdminDel.php?id='+id+'";// this should be delid, also bad quotes, also you want to set the href, not the location 
     return true; 
    } 
} 

修正:

function deleteme(delid) { 
    if(confirm("Do you want Delete!")){ 
     window.location.href="AdminDel.php?id=" + delid; 
     return true; 
    } 
} 
+0

@Nick_A - 它的100%完美工作.. 非常感謝! :d –