How to pass multiple JSON Objects as data using jQuery's $.ajax() ?
Last Updated :
30 Apr, 2021
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=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
</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.
Stylecss
.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 make a JSON call using jQuery ? Use the getJSON() function in jQuery to load JSON data. The getJSON() function uses a GET HTTP request to retrieve JSON-encoded data from the server. In this article, we will learn about the jQuery getJSON() function and its implementation through examples. Syntax: $(selector).getJSON(url, data, suc
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
4 min read
How to select values from a JSON object using jQuery ? In this article, we will select values from a JSON object and display them on the browser using jQuery. To select values from a JSON object to webpage, we use append() method. This append() method in jQuery is used to insert some content at the end of the selected elements. Syntax: $(selector).appen
2 min read
How to get server response from an AJAX request using jQuery ? In this article, we will see how we can use jQuery to get the server response to an AJAX request. The jQuery ajax() method implements the basic Ajax functionality in jQuery. It communicates with the server via asynchronous HTTP requests. Syntax:$.ajax(url);$.ajax(url,[options]);Parameters:url: A URL
4 min read
How to create clone of any object using jQuery ? In this article, we will learn to create a clone of an object using jQuery. This can be achieved using the extend() method of jQuery. The extend() method is used to merge the contents of multiple objects into the object passed as the first parameter. This can be used to clone an array as we can pass
3 min read