Approve And Disapprove in PHP MySQL Ajax

Throughout this tutorial, you will learn how to create an approval or accept and disapprove or reject system using php mysql & jQuery ajax.

How to Create Approve And Disapprove in Ajax, PHP & MySQL

Here are steps to create approve and disapprove system:

  • Step 1: Create MySQL Database & Table
  • Step 2: Set up the database
  • Step 3: Displaying the content From MySQL Table
  • Step 4: Create get_items.php File
  • Step 5: Approve And Disapprove in PHP MySQL Ajax
  • Step 6: Testing this System

Step 1: Create MySQL Database & Table

Run the following sql queries to create database and table for approval and disapprove system:

CREATE DATABASE DEMO;

CREATE TABLE items (
id INT(11) PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
approved TINYINT(1) DEFAULT 0
);

Step 2: Set up the database

Create database.php to set up the database connection for your php project:

<?php
// Database configuration
$host = 'localhost';
$username = 'your_username';
$password = 'your_password';
$dbname = 'your_database';

// Create a database connection
$conn = new mysqli($host, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

Step 3: Displaying the content From MySQL Table

Now, Create an HTML file with a form to display the items and allow the user to approve or disapprove them. So, create index.php file and update the following code into it:

<!DOCTYPE html>
<html>
<head>
<title>Approval System</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
// AJAX function to handle approval/disapproval
$('.approval-btn').click(function() {
var itemId = $(this).data('id');
var action = $(this).data('action');

$.ajax({
url: 'update_status.php',
method: 'POST',
data: {id: itemId, action: action},
success: function(response) {
// Handle the response
if (response.success) {
alert(response.message);
// Reload the item list
loadItems();
} else {
alert('An error occurred: ' + response.message);
}
},
error: function() {
alert('An error occurred while processing the request.');
}
});
});

// Load the initial item list
loadItems();
});

// AJAX function to load the item list
function loadItems() {
$.ajax({
url: 'get_items.php',
method: 'GET',
success: function(response) {
// Update the item list
$('#item-list').html(response);
},
error: function() {
alert('An error occurred while loading the items.');
}
});
}
</script>
</head>
<body>
<h1>Approval System</h1>

<div id="item-list"></div>
</body>
</html>

Step 4: Create get_items.php File

Now, let’s create the PHP files, which name get_items.php that will fetch data from MySQL database and display it in HTML list with approve and disapprove buttons:

<?php
require_once 'database.php';

// Retrieve items from the database
$sql = "SELECT * FROM items";
$result = $conn->query($sql);

// Generate the item list
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$itemId = $row['id'];
$title = $row['title'];
$approved = $row['approved'];

// Display the item and approval buttons
echo '<div>';
echo '<span>' . $title . '</span>';
echo '<button class="approval-btn" data-id="' . $itemId . '" data-action="approve">Approve</button>';
echo '<button class="approval-btn" data-id="' . $itemId . '" data-action="disapprove">Disapprove</button>';
echo '</div>';
}
} else {
echo 'No items found.';
}

Step 5: Approve And Disapprove in PHP MySQL Ajax

Create the PHP file, which is update_status.php that will handle the AJAX requests and update the item approval and disapproval status:

<?php
require_once 'database.php';

// Retrieve the item ID and action from the AJAX request
$itemId = $_POST['id'];
$action = $_POST['action'];

// Update the item approval status
if ($action === 'approve') {
  $sql = "UPDATE items SET approved = 1 WHERE id = $itemId";
} elseif ($action === 'disapprove') {
  $sql = "UPDATE items SET approved = 0 WHERE id = $itemId";
} else {
  echo json_encode(['success' => false, 'message' => 'Invalid action.']);
  exit;
}

if ($conn->query($sql) === TRUE) {
  echo json_encode(['success' => true, 'message' => 'Status updated successfully.']);
} else {
  echo json_encode(['success' => false, 'message' => 'Failed to update status.']);
}

Step 6: Testing this System

To test the approval system, ensure you have created the necessary PHP files and have the database connection details set up correctly. Then, open the HTML file in a web browser. You should see a list of items from the database with “Approve” and “Disapprove” buttons next to each item.

Conclusion

That’s it! You have successfully implemented an approval and rejection system using PHP, MySQL and AJAX.

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *