Approval and disapproval system using PHP, MySQL, and AJAX. Throughout this tutorial, you will learn how to create approval/accept and disapproval/reject system using php mysql and ajax jQuery.
And you can also use and modify approve and disapprove in ajax, jQuery, php & mysql free source to fit your specific requirements or enhance the user interface.
Approve And Disapprove in Ajax, PHP & MySQL
By using the following steps, you can create approve and disapprove in ajax, php & mysql:
- 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
First of all, open your phpmyadmin and execute the following SQL queries to create database and table for approval and disapprove system app in PHP MySQL ajax:
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
Next, create database.php to setup the database connection for your php app; so add the following code into your database.php app:
<?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
Next 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 disapproval system using PHP, MySQL, and AJAX. Feel free to modify the source code to fit your specific requirements or enhance the user interface.