The Wayback Machine - https://web.archive.org/web/20240527112701/https://www.geeksforgeeks.org/how-to-pass-multiple-json-objects-as-data-using-jquerys-ajax/
Open In App

How to pass multiple JSON Objects as data using jQuery’s $.ajax() ?

Last Updated : 30 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The purpose of this article is to pass multiple JSON objects as data using the jQuery $ajax() method in an HTML document.

Approach: Create a button in an HTML document to send JSON objects to a PHP server. In the JavaScript file, add a click event listener to the button. On clicking of the button, a request is made to PHP file using jQuery $ajax() method by which multiple JSON objects are passed to the server. 

HTML Code: The following code demonstrates the design or structure of the user interface. On click of the HTML button, it gives the response by the PHP server in the resultID HTML div.

index.html




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
  
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
  
    <!-- CSS file -->
    <link rel="stylesheet" href="style.css">
  
    <!-- jQuery Ajax CDN -->
    <script src=
    </script>
  
    <!-- JavaScript file -->
    <script src="script.js"></script>
</head>
  
<body>
     <center>
     <h2 style="color:green">GeeksforGeeks</h2>
     <div class="container">
        <b>Pass multiple JSON objects</b>
        <br/><br/>
        <!-- Button to multiple JSON objects -->
        <button type="button" id="btn">
            Click on me!
        </button>
        <div style="height:10px"></div>
        <div id="resultID"></div>
     </div>
     </center>
</body>
  
</html>


CSS Code: The following code is the content for the file “style.css” used in the above HTML code.

Style,css




.container {
  border: 1px solid rgb(73, 72, 72);
  border-radius: 10px;
  margin: auto;
  padding: 10px;
  text-align: center;
}
  
button {
  border-radius: 5px;
  padding: 10px;
  color: #fff;
  background-color: #167deb;
  border-color: #0062cc;
  font-weight: bolder;
  cursor: pointer;
}
  
button:hover {
  text-decoration: none;
  background-color: #0069d9;
  border-color: #0062cc;
}


JavaScript Code: The following code is the content for the file “script.js” used in the above HTML code. It handles the click() event for the button by using jQuery ajax() method and passing the data to a PHP server file i.e action.php

script.js




$(document).ready(() => {
      
  // Adding 'click' event listener to button
  $("#btn").click(() => {
        
    // Two JSON objects are passed to server    
    let obj1 = {"name": "John Doe"};
    let obj2 = {"name": "Duke"};
  
    // jQuery Ajax Post Request using $.ajax()
    $.ajax({
        url: 'action.php',
        type: 'POST',
        // passing JSON objects as comma(,) separated values
        data: {
            obj1,
            obj2
        },
        success: (response) => {
            // response from PHP back-end PHP server          
           $("#resultID").show().html(response);
        }
    })
  });
});


Note: You can pass as many JSON objects by using comma(,) separated values i.e. obj1, obj2, obj3,..

PHP code: The following is the code for the file “action.php” used in the above JavaScript code.

PHP




<?php
  
// Check if JSON Objects are set by user 
  
if (isset($_POST['obj1']) && $_POST['obj2'])
{
  // Get JSON objects in variables
   $obj1 = $_POST['obj1'];
   $obj2 = $_POST['obj2'];
    
  // Sending response to script file 
  echo "Success";
}
?>


Output :

multiple data passing and getting response



Similar Reads

How to pass both form data and credentials on submit in ajax ?
The purpose of this article is to send the form data and credentials to the PHP backend using AJAX in an HTML document. Approach: Create a form in an HTML document with a submit button and assign an id to that button. In JavaScript, the file adds an event listener to the form's submit button i.e click. Then the request is made to a PHP file using j
2 min read
Difference between Server-Side AJAX framework and Client-side AJAX framework ?
The article focuses on discussing the differences between the Server-side AJAX framework and the Client-side AJAX framework. The following topics will be discussed here: What is a Server-side AJAX framework?What is a Client-side AJAX framework?Server-side AJAX framework vs Client-side AJAX framework Let's start discussing each of these topics in de
3 min read
How to Send FormData Objects with Ajax-requests in jQuery ?
In this article, we will see how can we send formData objects with Ajax requests by using jQuery. To send formData, we use two methods, namely, the FormData() method and the second method is serialize() method. The implementation of Ajax to send form data enables the submission of data to the server without the need for a page refresh, which is a p
4 min read
How to use JSON in Ajax jQuery ?
Using JSON in AJAX requests with jQuery is a fundamental aspect of web development. JSON or JavaScript Object Notation, offers a lightweight and structured format for data exchange between a server and a web application. jQuery simplifies this process further through its AJAX functionalities. We will explore how to effectively use JSON in AJAX requ
3 min read
How to convert JSON string to array of JSON objects using JavaScript ?
Given a JSON string and the task is to convert the JSON string to an array of JSON objects. This array contains the values of JavaScript objects obtained from the JSON string with the help of JavaScript. There are two approaches to solve this problem which are discussed below: Approach 1: First convert the JSON string to the JavaScript object using
2 min read
How to Pass JSON Data in a URL using CURL in PHP ?
In this article, we will see how to pass the JSON Data in a URL using CURL in PHP, along with understanding the different ways for passing the data in a URL through the illustrations. The cURL stands for client URL, which allows us to connect with other URLs &amp; use their responses in our code, i.e., it is a tool for sending and getting files usi
7 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ?
In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. There are various methods to achieve this separation
4 min read
How to Remove Multiple Objects from Nested Array of Objects in JavaScript ?
A nested array of objects is an array of arrays that contain multiple objects as their elements. Removing multiple objects from the nested array of objects in JavaSript can be accomplished in different ways as listed below: Table of Content Using filter() with some() methodUsing filter() with includes() methodUsing filter( ) with findIndex() method
5 min read
How to use cURL to Get JSON Data and Decode JSON Data in PHP ?
In this article, we are going to see how to use cURL to Get JSON data and Decode JSON data in PHP. cURL: It stands for Client URL.It is a command line tool for sending and getting files using URL syntax.cURL allows communicating with other servers using HTTP, FTP, Telnet, and more. Approach: We are going to fetch JSON data from one of free website,
2 min read
Pass by Value and Pass by Reference in Javascript
In this article, we will talk about Pass by Value and Pass by Reference in JavaScript. Pass By ValueIn Pass by value, the function is called by directly passing the value of the variable as an argument. So any changes made inside the function do not affect the original value.In Pass by value, parameters passed as arguments create their own copy. So
4 min read