# Một ví dụ tuyệt vời Ajax SweetAlert for Live Data Deleting Rows in with PHP\~MySQL\~Ajax (ok)

![](/files/-M8YiZ7vTXI7zI_iiWjn)

{% embed url="<https://dbconfig.php>" %}

```
<?php
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "dbtest";
try {
  $DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname", $DBuser, $DBpass);
  $DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $ex) {
  die($ex->getMessage());
}
```

{% embed url="<https://dbtest.sql>" %}

{% file src="/files/-M8Yi0OEljRKyJeFaU63" %}

index.php

```
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Ajax SweetAlert for Live Data Deleting Rows in with PHP~MySQL~Ajax</title>
  <link rel="stylesheet" href="http://localhost/library/bootstrap/css/bootstrap.min.css" type="text/css" />
  <link rel="stylesheet" type="text/css" href="http://localhost/library/sweetalert2/sweetalert2.min.css"/>
  <link rel="stylesheet" type="text/css" href="http://localhost/library/fontawesome/css/all.min.css"/>
</head>
<body>
  <div class="container">
    <div class="page-header">
      <h1>
        <a target="_blank" href="http://www.lisenme.com/">Ajax SweetAlert for Live Data Deleting Rows in with PHP~MySQL~Ajax</a>
      </h1>
    </div>
    <div id="load-products"></div> <!-- products will be load here -->
  </div>
  <script type="text/javascript" src="http://localhost/library/jquery/jquery.min.js"></script>
  <script src="http://localhost/library/bootstrap/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="http://localhost/library/sweetalert2/sweetalert2.min.js"></script>
  <script>
  $(document).ready(function() {
    readProducts(); /* it will load products when document loads */
    $(document).on('click', '#delete_product', function(e) {
      var productId = $(this).data('id');
      SwalDelete(productId);
      e.preventDefault();
    });
  });
  function SwalDelete(productId) {
    swal({
      title: 'Are you sure?',
      text: "It will be deleted permanently!",
      type: 'warning',
      showCancelButton: true,
      confirmButtonColor: '#3085d6',
      cancelButtonColor: '#d33',
      confirmButtonText: 'Yes, delete it!',
      showLoaderOnConfirm: true,
      preConfirm: function() {
        return new Promise(function(resolve) {
          $.ajax({
              url: 'delete.php',
              type: 'POST',
              data: 'delete=' + productId,
              dataType: 'json'
            })
            .done(function(response) {
              swal('Deleted!', response.message, response.status);
              readProducts();
            })
            .fail(function() {
              swal('Oops...', 'Something went wrong with ajax !', 'error');
            });
        });
      },
      allowOutsideClick: false
    });
  }

  function readProducts() {
    $('#load-products').load('read.php');
  }
  </script>
</body>

</html>
```

{% file src="/files/-M8YiPmJWda\_MjgNCADg" %}

read.php

```
<div class="table-responsive">
  <table class="table table-bordered table-condensed table-hover table-striped" cellspacing="0" width="100%">
    <thead>
      <tr>
        <th>ID</th>
        <th>Application</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <?php
				require_once 'dbconfig.php';
				$query = "SELECT product_id, product_name FROM tbl_products";
				$stmt  = $DBcon->prepare($query);
				$stmt->execute();
				if ($stmt->rowCount() > 0) {
  				while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    			extract($row);
		    		?>
				      <tr>
				        <td><?php echo $product_id; ?></td>
				        <td><?php echo $product_name; ?></td>
				        <td><a class="btn btn-sm btn-danger" id="delete_product" data-id="<?php echo $product_id; ?>" href="javascript:void(0)"><i class="far fa-trash-alt"></i></a></td>
				      </tr>
		      <?php
					}
				} else { ?>
		      <tr>
		        <td colspan="3">No Products Found</td>
		      </tr>
	      <?php
					}
				?>
    </tbody>
  </table>
</div>
```

{% embed url="<https://delete.php>" %}

```
<?php
header('Content-type: application/json; charset=UTF-8');
$response = array();
if ($_POST['delete']) {
	require_once 'dbconfig.php';
	$pid = intval($_POST['delete']);
	$query = "DELETE FROM tbl_products WHERE product_id=:pid";
	$stmt = $DBcon->prepare( $query );
	$stmt->execute(array(':pid'=>$pid));
	if ($stmt) {
		$response['status']  = 'success';
		$response['message'] = 'Product Deleted Successfully ...';
	} else {
		$response['status']  = 'error';
		$response['message'] = 'Unable to delete product ...';
	}
	echo json_encode($response);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/advanced/mot-vi-du-tuyet-voi-ajax-sweetalert-for-live-data-deleting-rows-in-with-php-mysql-ajax-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
