Hello, I would like to create a confirmation box for delete. Do you want to delete this row? [Yes] [No]

How?

How to create a colorful confirmation box? ok, I find the demo link demo link

NOw, I wonder how to merge php with jquery ? So if someone click Yes it will delete the particular row, otherwise it will be cancelled.

studentmgt.php

<div id="inputberita">
      <center>
        <div id="title"><h2>Student Management</h2></div>
      </center>


      <p>&nbsp;</p>

  <p>&nbsp;</p><center>


<?php         

      include('../../includes/koneksi.php');



        //Hapus berita // undefined index: mode



    if (!empty($_REQUEST['student_id']))
        {
        $student_id = $_REQUEST['student_id'];

         // Query to get student picture filename
        $result = mysql_query("SELECT newfilename FROM student WHERE student_id =".$student_id) or die(mysql_error());
        $data1 = mysql_fetch_assoc($result);
        $pic_filename = $data1['newfilename'];

        $result = mysql_query("DELETE FROM student WHERE student_id =".$student_id) or die(mysql_error());

        if($result){
            unlink('../upload/'.$pic_filename);
        }

        $confirmation = !$result ? "Fail to delete data." : "Data has been deleted.";

        }

    if(!empty($_GET['search'])){
       $search = $_GET['search'];
       }
    else
       {
       //$search = "";
       }
?>
<div align="center">
    <div style="width:700px;text-align:left;padding-top:5px;">
        <?php  if (isset($confirmation)) { echo $confirmation; } ?>  
        <form method="get" action="<?php $_SERVER['PHP_SELF'] ?>">

                                    <br/>

                <br><br>

                <?php

                // pagination references:
                // http://www.raymondselda.com/php-pagination-script/

                //Include the PS_Pagination class
                include('ps_pagination.php');

                //Connect to mysql db
                $conn = mysql_connect('localhost','root','');
                mysql_select_db('squprime',$conn);

                if(!empty($search)){

                $sql = "SELECT * FROM `student`
                INNER JOIN `group`
                ON group.group_id = student.group_id
                INNER JOIN `access`
                ON access.access_level_id = student.access_level_id WHERE group.group_name = '".$search."' OR student.stu_fname = '".$search."' OR stu_lname = '".$search."' OR student.stu_address = '".$search."' OR student.stu_hp = '".$search."' OR access.access_level = '".$search."' ORDER BY group_name ASC";

                }else{

                $sql = "SELECT * FROM `student`
                INNER JOIN `group`
                ON group.group_id = student.group_id
                INNER JOIN `access`
                ON access.access_level_id = student.access_level_id";

                }

                //Create a PS_Pagination object
                $pager = new PS_Pagination($conn,$sql,10,10);


                ?>


                Search: <input type="text" size="20px" name="search" value=""><br><br>

                <button type="button" onClick="parent.location='studentinfo.php'">Add New Student</button><br><br>


                <table id="admintable" border="1" cellpadding="2" cellspacing="0" width="900">
                    <tr>
                        <th>Group</th><th>First Name</th><th>Last Name</th><th>Address</th><th>hp</th><th>Access</th><th>Post</th>
                    </tr>
                <?php
                $i=0;
                //The paginate() function returns a mysql result set 
                $result = $pager->paginate();

                while($data = mysql_fetch_assoc($result)) {
                    $result2=($i%2)?'#DFA09D':'white';

                    echo "<tr bgcolor='$result2'>";

                    echo '<td>'.$data['group_name'].'</td>';      
                    echo '<td>'.$data['stu_fname'].'</td>';
                    echo '<td>'.$data['stu_lname'].'</td>';
                    echo '<td>'.$data['stu_address'].'</a></td>';
                    echo '<td>'.$data['stu_hp'].'</a></td>';
                    echo '<td>'.$data['access_level'].'</a></td>';
                    echo '<td><a href="studentmgt.php?student_id='.$data['student_id'].'">Delete</a></td>';
                    echo '</tr>';

                    $i++;
                    }


                ?>
                </table>

                <?php //Display the full navigation in one go
                echo '<br><br>';
                echo $pager->renderFullNav();
                ?>

        </form>
    </div>
</div>
  </center>
</div>  


</div>

Thanks before.

Just call the plugin in your demo link, and use the following snippets.

Ajust your PHP:

<a href="studentmgt.php?student_id='.$data['.student_id.'].'" class="confirm">Delete</a>

jQuery:

$(".confirm").confirm();

Then just put some PHP/SQL in the link to delete the record.

Thanks for the advice.

Yet, I still do not understand how delete the data after the confirmation.

I cannot insert sql_query() right in // do something?

<script>
$(".confirm").confirm({
    text: "Are you sure you want to delete that comment?",
    title: "Confirmation required",
    confirm: function(button) {
        // do something
    },
    cancel: function(button) {
        // do something
    },
    confirmButton: "Yes I am",
    cancelButton: "No",
    post: true
});

</script>

<button class="confirm" type="button">Delete the comment</button>

SQL cannot in any way be executed by Javascript. Bad idea.

Instead, you can use AJAX:

$.ajax({
  type: "POST",
  url: "delete.php",
  data: { comment_id: 12345 }
})
  .done(function() {
    alert( "Comment successfully deleted" );
  });

The above code will send off the comment id using the post method via AJAX. All you need to do is shove this into your code above, change the comment_id to the actual id of the comment you're deleting, and write delete.php to take the $_POST variable and delete the comment using SQl.

You can simply do in this way.

<a onClick="return confirm('Are you sure you want to delete this')" href="#">Delete</a>

@Sikander : I did try -

echo '<td><a href="studentmgt.php?student_id='.$data['student_id'].'" onClick="return confirm("Are you sure you want to delete this")>Delete</a></td>';

Nothing happen, no alert - the row is deleted directly.

@mattster :

I also try this:

echo '<td><a href="studentmgt.php?student_id='.$data['student_id'].'")>Delete</a></td>';
                    echo '</tr>';

                    $i++;
                    }

                $.ajax({
                type: "POST",
                url: "delete.php",
                data: { comment_id: 12345 }
                      })
                .done(function() {
                alert( "Comment successfully deleted" );
                });

I do not know how to incorporate/call the function inside the echo syntax.

echo '<td><a href="studentmgt.php?student_id='.$data['student_id'].'" onClick="return confirm("Are you sure you want to delete this")>Delete</a></td>';

Will never work, because your quotes are mixed up. Try this:

echo '<td><a href="studentmgt.php?student_id='.$data['student_id'].'" onClick="return confirm(\'Are you sure you want to delete this?\')">Delete</a></td>';

And for Javascript, you just shove it at the bottom of the html page, just before the </body> with the jQuery library. If you're unfamiliar with AJAX and such like, try using @Sikander Nasar's idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.