简体   繁体   English

使用javascript单击编辑按钮时如何弹出消息

[英]How to pop up message when click on edit button using javascript

I want to popup message box when press edit button on my php page. 我想在我的php页面上按“编辑”按钮时弹出消息框。 After confirmation should pass data to edit page. 确认后应将数据传递到编辑页面。 now data is parsing to edit page without showing message. 现在,数据正在解析以编辑页面,而不会显示消息。 Please help me to solve this. 请帮我解决这个问题。

<?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!";
}
?>

what you are looking for is a confirm dialog. 您正在寻找的是一个确认对话框。 Based on the choise you can stop the submit 根据选择,您可以停止提交

$('form').submit(function(event){
    if(!confirm('are you sure?')){
        event.preventDefault();
        return false;
    }
});

https://jsfiddle.net/2hckck96/ https://jsfiddle.net/2hckck96/

1st : Remove unnecessary escape character . 第一:删除不必要的转义字符。

2nd : Missed starting double quotes for href attribute . 2号: href属性缺少起始双引号。

code : Final code should be . 代码:最终代码应该是。

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>';

Normally you would like to separate your HTML and Javascript, so I suggest you don't use inline event handlers, but put a class on your EDITLINK and add an event listener to it. 通常,您希望将HTML和Javascript分开,所以我建议您不要使用内联事件处理程序,而应在EDITLINK上放置一个并为其添加事件侦听器。

Your Edit link has been created dynamically so you need a Delegate Event 您的Edit链接已动态创建,因此您需要一个Delegate Event

Please find below example how to do that 请在下面的示例中找到如何操作

$('body').on('click', '.EDITLINKCLASS', function () {
   if (!confirm('Are you sure?')) e.preventDefault();
});

This is more efficient than inline handlers. 这比内联处理程序更有效。

Use jQuery 使用jQuery

  1. add class to links 将类添加到链接

    <a href="#" class="lnkDelComp">Delete</a>

  2. add the jQuery 添加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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM