Using AJAX to perform CRUD operations on a database in JavaScript

In modern web development, it is common to perform CRUD (Create, Read, Update, Delete) operations on a database using AJAX (Asynchronous JavaScript and XML). AJAX allows us to send and receive data from a server without having to reload the entire webpage.

In this tutorial, we will use JavaScript to implement AJAX calls for performing CRUD operations on a database.

Prerequisites

Technologies used

Step 1: Set up the database

First, we need to set up the database with a table (if using a SQL-based database) or collection (if using a NoSQL database) to store our data. Make sure you have the necessary permissions and configurations in place.

For example, if using MySQL, you can create a table called “users” with columns such as “id”, “name”, and “email”.

Step 2: Create HTML form

Next, we will create an HTML form to capture user input for creating or updating records. The form should have input fields for each necessary field in the database table or collection.

<form id="userForm">
  <input type="text" id="nameInput" placeholder="Name">
  <input type="email" id="emailInput" placeholder="Email">
  <button type="submit">Submit</button>
</form>

Step 3: Handle form submission

Using JavaScript, we will handle the form submission and make an AJAX call to either create or update a record in the database.

document.getElementById("userForm").addEventListener("submit", function(event) {
  event.preventDefault(); // Prevent form from submitting
  
  var name = document.getElementById("nameInput").value;
  var email = document.getElementById("emailInput").value;
  
  // Make AJAX call to create/update record
  var xhr = new XMLHttpRequest();
  xhr.open("POST", "/api/users", true); // Replace with your backend API endpoint
  xhr.setRequestHeader("Content-Type", "application/json");
  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
      // Handle successful response
      console.log(xhr.responseText);
    }
  };
  xhr.send(JSON.stringify({ name: name, email: email }));
});

Step 4: Implement read, update, and delete operations

To implement the read, update, and delete operations, we can make similar AJAX calls to the appropriate API endpoints and handle the responses accordingly.

For example, to read all users from the database:

var xhr = new XMLHttpRequest();
xhr.open("GET", "/api/users", true); // Replace with your backend API endpoint
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var users = JSON.parse(xhr.responseText);
    // Handle the retrieved users
    console.log(users);
  }
};
xhr.send();

To update a user in the database:

var xhr = new XMLHttpRequest();
xhr.open("PUT", "/api/users/{userId}", true); // Replace {userId} with the actual user ID
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    // Handle successful response
    console.log(xhr.responseText);
  }
};
xhr.send(JSON.stringify({ name: newName, email: newEmail }));

To delete a user from the database:

var xhr = new XMLHttpRequest();
xhr.open("DELETE", "/api/users/{userId}", true); // Replace {userId} with the actual user ID
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    // Handle successful response
    console.log(xhr.responseText);
  }
};
xhr.send();

Conclusion

By using AJAX in JavaScript, we can easily perform CRUD operations on a database without having to reload the entire webpage. This allows for a smoother and more interactive user experience. Remember to properly validate and sanitize user input to prevent security vulnerabilities.