簡體   English   中英

使用 ajax 將 php 數組傳遞給另一個 php 文件

[英]Pass an php array to another php file with ajax

我想發送一個數組,這是我從 mysql 獲得的,並將它與 ajax 一起發送到另一個 php 文件。 我無法將數據發送到文件。 我在第一個 PHP 文件中獲取數據。 但是當我在 JS 中使用數組設置警報時,output 是未定義的。 另一個 PHP 文件沒有收到任何數據。

PHP 發送到另一個 PHP 文件

   <?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 應該獲取數組的文件

<?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 ?>'},

是錯誤的地方,改成

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

base64_encodeurl_encode json 輸出以使其 url 友好

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM