0

I want to popup message box when press edit button on my php page. 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!";
}
?>
serverAdmin123
  • 115
  • 1
  • 13
  • Use a modal which comes with bootstrap.. – SilentCoder Aug 29 '17 at 11:21
  • 1
    simple use `confirm('are you sure you want edit ?')` on click of edit button – JYoThI Aug 29 '17 at 11:21
  • This is your edit page right can you please post the code of your page where you pass the data to this. – S4NDM4N Aug 29 '17 at 11:21
  • You've got several syntax errors in your edit link - the `href` attribute has no opening quote, the escaping of the other quotes is wrong, and you don't close the `` tag. – iainn Aug 29 '17 at 11:23
  • @sand no this i want to edit one single row in this page. this page retrieve data from database. There is a problem with below line. i want to rectify this row only. ` code Edit]` – serverAdmin123 Aug 29 '17 at 11:24
  • Possible duplicate of [How to show a confirm message before delete?](https://stackoverflow.com/questions/9139075/how-to-show-a-confirm-message-before-delete) – S4NDM4N Aug 29 '17 at 11:24
  • 1
    `'%".$_POST["search"]."%'` is vulnerable to SQL injection attacks. You should **never** inject un-sanitised and un-parameterised variables directly into your SQL string. See http://bobby-tables.com for an explanation of the potential risk and examples of how to write your queries safely in PHP. As it stands you're wide open to a hacker stealing, corrupting or even destroying your data, not to mention the potential for unexpected syntax errors simply crashing your app. – ADyson Aug 29 '17 at 11:24
  • `code Edit ` I have problem With this line – serverAdmin123 Aug 29 '17 at 11:28

5 Answers5

0

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/

Victor Radu
  • 2,262
  • 1
  • 12
  • 18
0

1st : Remove unnecessary escape character .

2nd : Missed starting double quotes for href attribute .

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>';
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

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.

Your Edit link has been created dynamically so you need a 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.

Curiousdev
  • 5,668
  • 3
  • 24
  • 38
0

Use jQuery

  1. add class to links

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

  2. add the 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!");
                }
            });
        });
    });
    

Orient Developers
  • 139
  • 1
  • 1
  • 10
0

try this

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