N
N
Nekio2021-07-20 10:38:45
AJAX
Nekio, 2021-07-20 10:38:45

How to pass several arrays to 1 php file from js using AJAX?

For example, there are such arrays in JS:

let arr1 = [1, 2, 3, 4, 5];
let arr2 = ['a', 'b', 'c', 'd', 'e'];

and file test.php

How to accept them in PHP so that they have a different ID.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nadim Zakirov, 2021-07-20
@QuayS1de

This is all done quite simply. Below is an example of sending data using jQuery.ajax()

async function sendInfo() {

  let arr1 = [1, 2, 3, 4, 5];
  let arr2 = ['a', 'b', 'c', 'd', 'e'];

  var response = await $.ajax({
    url: 'test.php',
    method: 'POST',
    cache: false,
    data: {
      'arr1': arr1,
      'arr2': arr2
    }
  });

  console.log('Данные успешно отправлены, ответ сервера: ' + response);
  
  return response;

}

Just call the sendInfo() function and see the result of the request in the console.
I should warn you that for the jQuery.ajax() function to work , you must have the jQuery library connected, for this, add the following script before the closing tag on your site:</head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

PS On the PHP side of the script, you need to look at the $_POST['arr1'] and $_POST['arr2'] variables - this is where the data you sent will be stored.

A
Alexey Dubrovin, 2021-07-20
@alekcena

Make a wrap?

let a = { 
mas1:[1,2,4,5,6,],
mas2:['asd','as2']
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question