Home  >  Article  >  Backend Development  >  How to return array object to AJAX request in php

How to return array object to AJAX request in php

PHPz
PHPzOriginal
2023-04-20 13:53:12644browse

PHP can convert the array object into a JSON string through the json_encode function and return it to the front-end page through AJAX. In this article, we will explain how to convert an array object into a JSON string using PHP and return it to an AJAX request.

1. Use PHP to convert array objects to JSON strings

In PHP, you can use the json_encode function to convert array objects to JSON strings. The json_encode function converts a PHP array or object into a JSON-formatted string. Below is a simple PHP array:

$data = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

You can use the following code to convert the above array to a JSON string:

echo json_encode($data);

This will output the following JSON string:

{"name":"John","age":30,"city":"New York"}

2. Use AJAX request to return JSON string from PHP to the front-end page

The following is a simple example showing how to use AJAX request to return JSON string from PHP:

Front-end page :

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>PHP给AJAX返回JSON格式的数据</title>
  </head>
  <body>
    <div id="status"></div>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script>
      $(document).ready(function() {
        $.ajax({
          type: "POST",
          url: "ajax.php",
          success: function(response) {
            var data = JSON.parse(response);
            $("#status").html("Name: " + data.name + "<br>Age: " + data.age + "<br>City: " + data.city);
          }
        });
      });
    </script>
  </body>
</html>

In the above code, we use jQuery's ajax function to send a POST request to the ajax.php file, and update the tag in the HTML page when successful.

ajax.php file content:

$data = array(
    "name" => "John",
    "age" => 30,
    "city" => "New York"
);

echo json_encode($data);

When the ajax.php file is accessed, it will output data in JSON format. The JSON format response received by the ajax function is saved in the response variable. On success, we use the JSON.parse function to parse the JSON response data into a JavaScript object and use jQuery to update the tag in the HTML page.

Summary

This article explains how to use PHP to convert an array object into a JSON string and return it to an AJAX request. In this way, you can use PHP to dynamically generate data and return it to the user via AJAX, enabling more flexible and dynamic web pages. Hope this article can be helpful to you.

The above is the detailed content of How to return array object to AJAX request in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn