Home > Backend Development > PHP Tutorial > How to Send a JavaScript Array to PHP Using jQuery AJAX?

How to Send a JavaScript Array to PHP Using jQuery AJAX?

Barbara Streisand
Release: 2024-11-25 07:30:12
Original
512 people have browsed it

How to Send a JavaScript Array to PHP Using jQuery AJAX?

Posting JavaScript Array to PHP Using jQuery AJAX

To pass a JavaScript array to PHP using jQuery's $.ajax method, follow these steps:

Problem:

In your code, you're assigning the JavaScript array activities to the data option directly as a string:

data: "activitiesArray="+activities,
Copy after login

This method is incorrect as it attempts to send the array as a single string value rather than an array of individual elements.

Solution:

To properly send a JavaScript array to PHP through jQuery AJAX, use the data option as an object:

data: { activitiesArray: activities },
Copy after login

By using an object, each element of the activities array will be converted into a key-value pair, where the key is the element's name and the value is the element's value.

PHP Access:

In PHP, you can access the array using the $_REQUEST superglobal:

<?php
$myArray = $_REQUEST['activitiesArray'];
?>
Copy after login

This will give you an array containing the elements of the JavaScript activities array.

The above is the detailed content of How to Send a JavaScript Array to PHP Using jQuery AJAX?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template