There is an HTML table which I put in an array on JS. Next, you need to transfer the array from JS to PHP array for further work.

enter image description here

This code puts the entire table data into an array in JS:

var myArray = new Array(); $(document).ready(function() { var table = $('.products__table'); var tr = table.children().children().not('tr:first-child'); $(tr).each(function(i) { myArray.push($(this).text()); }); console.log(myArray); $('#send').click(function() { for (var i = 0; i < myArray.length; i++) { window.location.href = "admin.php?name=" + myArray[i]; } }); }); 

console.log (myArray) returns everything correctly. enter image description here

 $test = $_GET['name']; echo $testi; 

But it gives only the last element.

enter image description here

What is the problem? Thank.

  • do var_dump - Anton Kucenko
  • string (73) "9 Armature 101620 Frunze. Kirgiziyatn.56375005412000" - Sherzod Norov
  • And what do you expect if you have a window of the form window.location.href = "admin.php?name=" + myArray[i]; ? Naturally, the last opening URL will contain the last element of the array. - u_mulder Nov.

1 answer 1

You need to send an array to the server using the .post () or .get () or .ajax () method. And on the server side, get the ready array and work with it through the cycle. Example posting with .post ():

  $('#send').click(function() { let url = 'admin.php'; $.post(url,myArray:myArray,function(date){ // data - данные с сервера. }); }); 
 $myArray = $_POST['myArray']; 
  • do not tell me what to write in // data - data from the server.? - Sherzod Norov
  • @SherzodNorov In this variable server response. In your example, we ask that the admin.php file be executed and the result of its work will be passed to the data variable. To see what is in data, the value of the variable must be added to html. You can do it like this: $ ('body'). Html (data) ;. In the body tag you will see the value (data) of the data variable. - doox911
  • @Sherzodorov My answer is the solution to your problem? - doox911
  • Could not do as in your example. But anyway, thanks for the help. Solved the problem differently. Did everything in php. But this is another topic - Sherzod Norov