简体   繁体   中英

Pass an php array to another php file with ajax

I want to send an array, which I got from mysql, and send it with ajax to another php file. I can't send the data to the file. I get the data in the first PHP file. But when I set an alert with the array in JS then the output is undefined. The other PHP file didn't received any data.

PHP send to another PHP file

   <?php

   $id = $_GET['id'];
   $items = mysqli_query($con,"SELECT * FROM COMPARISONFOLDER JOIN ITEM ON ITEM.COMPARISONFOLDER_ID LIKE COMPARISONFOLDER.COMPARISONFOLDER_ID WHERE COMPARISONFOLDER.COMPARISONFOLDER_ID LIKE $id");

   while($item = mysqli_fetch_array($items)) {
       $shuffledItems[] = $item;
   }
   shuffle($shuffledItems);
?>
<html>
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <script type="text/javascript">

   jQuery(document).ready(function(){
       getItems();
   });

   function getItems() {
         var urlActiveFillSurvey = "https://vinkovic.ch/test1/activeFillSurvey.php/";
         jQuery.ajax({
             type: 'GET',
             url: urlActiveFillSurvey,
             data: {data : '<?php echo $shuffledItems ?>'},
             processData: false,
             contentType: false,
             success: function(data) {
               jQuery('.data').html(data.responseText);
             },
             error: function(data) {
               jQuery('.data').html(data.responseText);
             }
         });
       }
   </script>
</head>
<body>
<div class="data"></div>
</body>
</html>

PHP File which should get the array

<?php
require ('../wp-blog-header.php');
$data = $_GET['data'];
echo "<table border='1'>
    <tr>
    <th>Item 1</th>
    <th>Item 2</th>
    </tr>";

    echo "<tr>";
    echo '<td><img src="data:image/jpeg;base64,'.base64_encode($data[0]['ITEM']).'" width="500" height="auto"/></td>';
    echo '<td><img src="data:image/jpeg;base64,'.base64_encode($data[1]['ITEM']).'" width="500" height="auto"/></td>';
    echo "</tr>";

echo "</table>";
data: {data : '<?php echo $shuffledItems ?>'},

is where the error is, change to

data: {data : '<?php echo json_encode($shuffledItems) ?>'},

or base64_encode or url_encode the json out put to make it url friendly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM