我正在尝试将数据列表从 javascript 数组传输到 php 数组,但我似乎无法让 ajax 来解决问题。有人可以告诉我如何做到这一点的代码吗?到目前为止,这就是我所拥有的,这只是数组:
JAVASCRIPTvar dates;
// the dates array is then filled up here
// it contains information in the form {'2012-01-01', '2012-03-12', ...}
$.ajax({
type: "REQUEST",
url: "NAMEOFPHPFILE.php",
data: { sendData1 : dates },
success: function() {
alert("Attempting to transfer array -> AJAX");
}
});
var submissions;
// the submissions array is then filled up here
// it contains information in the form {int, int, ...}
// ect ......... with 3 more arrays using { sendData2 : submissions },
PHP
<?php
$bDates = $_REQUEST['sendData1'];
// need to set this array = to the JavaScript dates array
$bSubmissions = $_REQUEST['sendData2'];
// need to set this array = to the JavaScript submissions array
?>
我更喜欢使用 REQUEST 方法来防止信息登录到 URL。 尝试 POST 而不是 REQUEST 时,此代码也不起作用
在 .php 页面的最后,我将一堆数组输出到一个 CSV 页面上,在该页面中我遍历数组并将它们的元素放入文件中。这已经有效,但我需要将其中一些数组从 javascript 传输到 PHP,以便我可以吐出数据。看起来像这样:
<?php
$stringData = "Dates, Number of Lines Changed, , Bug Dates, Arrivals, Fixed, Closed, Hold_";
for($i = 0; $i < sizeof($dataXAxis); $i++){
$date = substr($_REQUEST['Dates'][$i+1], 0, 24);
$stringData .= "$date, $dataYAxis[$i], , $bDates[$i], $bSubmissions[$i], $bCompletions[$i], $bDones[$i], $bRejections[$i]_";
}
echo '<BR><BR><a href="download_csv.php?csv=' . $stringData . '" target="_blank">Download Your CSV File</a>';
?>
为什么 AJAX 不起作用?数组显示为空...