[英]How to pop up message when click on edit button using javascript
我想在我的php頁面上按“編輯”按鈕時彈出消息框。 確認后應將數據傳遞到編輯頁面。 現在,數據正在解析以編輯頁面,而不會顯示消息。 請幫我解決這個問題。
<?php
include 'connection.php';
//var_dump($_POST['search']);
$query= "select * from computer where lab_no like '%".$_POST["search"]."%'";
//var_dump($query);
$output='';
$result= mysqli_query($conn, $query);
if(mysqli_num_rows($result)>0){
$output.='
<table class = "table table-bordered table-hover">
<tr>
<th style="text-align:center">Computer Number</th>
<th style="text-align:center">Computer Name</th>
<th style="text-align:center">Brand</th>
<th style="text-align:center">Edit</th>
<th style= "text-align:center">Delete</th>
</tr>';
while($row= mysqli_fetch_array($result)){
$output.='
<tr>
<td style= "text-align:center">'.$row["token_number"].'</td>
<td style="text-align:center">'.$row["com_name"].'</td>
<td style="text-align:center">'.$row["brand"].'</td>
<td style="text-align:center"><a href=../htdocs/add-com_edit.php?com_num=' . $row["token_number"] . ' \"onclick=\"return confirm("Are You Sure to Update this Record?");\"><span class="btn-success form-control" style="text-align:center">Edit</span></td>
<td style="text-align:center"><a href=\"../htdocs/po_edit.php?ponum=" . $row["token_number"] . "\" onclick=\"return confirm("Are You Sure to Delete this Record?");\"><span class="btn-danger form-control" style="text-align:center">Delete</span></td>
<tr/>';
}
echo $output;
$output.='</table>';
}else{
echo"No Data Found!";
}
?>
您正在尋找的是一個確認對話框。 根據選擇,您可以停止提交
$('form').submit(function(event){
if(!confirm('are you sure?')){
event.preventDefault();
return false;
}
});
第一:刪除不必要的轉義字符。
2號: href屬性缺少起始雙引號。
代碼:最終代碼應該是。
echo '<td style="text-align:center"><a href="../htdocs/add-com_edit.php?com_num=' . $row["token_number"] . '" onclick="return confirm(\'Are You Sure to Update this Record? \');"><span class="btn-success form-control" style="text-align:center">Edit</span></td>';
通常,您希望將HTML和Javascript分開,所以我建議您不要使用內聯事件處理程序,而應在EDITLINK上放置一個類並為其添加事件偵聽器。
您的Edit
鏈接已動態創建,因此您需要一個Delegate Event
請在下面的示例中找到如何操作
$('body').on('click', '.EDITLINKCLASS', function () {
if (!confirm('Are you sure?')) e.preventDefault();
});
這比內聯處理程序更有效。
使用jQuery
將類添加到鏈接
<a href="#" class="lnkDelComp">Delete</a>
添加jQuery
<script> $(document).ready(function(){ $('.lnkDelComp').each(function(){ $(this).click(function(){ var comNum = $(this).closest('tr').('td:eq(0)').text(); var ask = confirm("Are you sure you want to delete "+comNum+"?"); if(ask){ window.location = "/path/to/add-com_edit.php?com_num="+comNum; } else{ alert("You have cancelled!"); } }); }); });
嘗試這個
<td style=\"text-align:center\"><a href=\"../htdocs/add-com_edit.php?com_num=".$row["token_number"]."\" onclick=\"return confirm('Are You Sure to Update this Record?');\"><span class=\"btn-success form-control\" style=\"text-align:center\">Edit</span></td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.