0

Hello am trying to pass an array from my JS script file to another file called serverSide.php so i can print the array in that file and do something with it , am having no issue sending the data and getting a response using the AJAX/JSON the problem i am getting "Notice: Undefined index: theArray in C:\xampp\htdocs\Ajax test\serverSide.php on line 2" when i try to print_r the array i sent ?

**Here is my JS code **

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script
    src="https://code.jquery.com/jquery-3.5.1.min.js"
    integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
    crossorigin="anonymous"></script>
</head>
<body>
    <h1>Testing AJAX techniques</h1>
<script>
    let myArray = [1,2,3];
    
    console.log(myArray);
    let myArray1 = JSON.stringify(myArray);
    console.log(myArray1);

    $.post({
      method: 'POST',
      url: 'serverSide.php',
      data: {theArray: myArray1},
      success: function(res) {
        console.log(res);
      }
    })
       
</script>
</body>
</html>

And this is the serverSide.php file code where i try to print the array

    <?php 
     $test = json_decode($_POST['theArray']);
     print_r($test);
?>

Thank you for any help

  • have you tried to `var_dump($_POST)`? And see what is inside it – Jwan Jan 23 '21 at 11:42
  • yep tried it now and it gives me this array(0) { } – Zakaria Hilali Jan 23 '21 at 11:45
  • you do not have a JSON object to decode yet using `json_decode`. The ajax function is sending a json string so php treats it as a string – Professor Abronsius Jan 23 '21 at 11:53
  • is the `serverSide.php` on the same directory as the index? – Jwan Jan 23 '21 at 11:58
  • yes it is in the same directory – Zakaria Hilali Jan 23 '21 at 11:59
  • can you copy the path of the index file and paste it here? – Jwan Jan 23 '21 at 12:00
  • Sure here http://localhost/ajax%20test/index.php , all my trying to do is find a way to the array printed in the other file – Zakaria Hilali Jan 23 '21 at 12:04
  • Try my answer. And also try to open the `serverSide.php` file in the browser and in the file echo something out – Jwan Jan 23 '21 at 12:07
  • This way you would be sure, that you have the correct path – Jwan Jan 23 '21 at 12:08
  • i fixed the path as the answer below suggests and yours as well , also i echoed a string and it is fine i am getting another notice now which is Notice: Undefined index: theArray in C:\xampp\htdocs\Ajax_test\serverSide.php on line 2 , thats when i try to put this code – Zakaria Hilali Jan 23 '21 at 12:18
  • try to var_dump($_POST) and see what is inside it – Jwan Jan 23 '21 at 12:22
  • all i get is an empty array – Zakaria Hilali Jan 23 '21 at 12:56
  • You're sending JSON inside a URL-encoded variable. You might be able to make it work but it's a bit daft, and it's not impossible it could corrupt the data sometimes. Instead send actual JSON - set the content type of the request to application/JSON and send just the JSON string. Then refer to this article for how to receive the JSON on the PHP side: https://stackoverflow.com/questions/18866571/receive-json-post-with-php – ADyson Jan 23 '21 at 13:02

0 Answers0