...
if(!empty($_POST)){  $output[\\'message\\'] = \\\"Success!\\\";  echo json_encode($output);}

Explanation:

By following this approach, you can successfully send and process Javascript arrays via POST requests and receive the results in your PHP script.

","image":"http://www.luping.net/uploads/20250322/174262428667de561e810cd.jpg174262428667de561e810d4.jpg","datePublished":"2025-03-23T13:15:23+08:00","dateModified":"2025-03-23T13:15:23+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"

Why Isn\'t My JavaScript Array Sending to PHP via POST?

Posted on 2025-03-23
Browse:728

Why Isn\'t My JavaScript Array Sending to PHP via POST?

Javascript POST Not Functioning: Sending Javascript Array to PHP

Problem:

A Javascript array cannot be sent to a PHP script using POST. The POST request fails to pass the array data.

Resolution:

The issue stems from a misunderstanding of the AJAX workflow. While jQuery simplifies the process, it does not automate it entirely. To send an array to PHP and display the output:

  • index.php

...
  • catcher.php
if(!empty($_POST)){
  $output['message'] = "Success!";
  echo json_encode($output);
}

Explanation:

  • The HTML page includes a text input and a button.
  • When the button is clicked, it retrieves the text from the input.
  • If the text is not empty, an AJAX POST request is initiated using jQuery.
  • The request sends the text to the catcher.php script.
  • catcher.php handles the request, processes the data, and returns a JSON response with a success message.
  • The AJAX callback function in index.php receives the response and sets it as the text for the #response element on the page.

By following this approach, you can successfully send and process Javascript arrays via POST requests and receive the results in your PHP script.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3