0

我正在尝试将数据列表从 javascript 数组传输到 php 数组,但我似乎无法让 ajax 来解决问题。有人可以告诉我如何做到这一点的代码吗?到目前为止,这就是我所拥有的,这只是数组:

JAVASCRIPT
var 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 不起作用?数组显示为空...

4

5 回答 5

3

一种方法是尝试以 JSON 的形式发送数据。然后使用 json_decode,可以将其转换为数组。例子:

   var Json = {"User1":"John", "User2":"Joe", "User3","Jerry"};
   var data = "data="+Json;
   hr = new XMLHttpRequest();
   hr.onreadystatechange = function(){
       if(hr.readyState == 4 && hr.status == 200){
           console.log(hr.responseText);
       }
   }
   hr.open("POST", "parsefile.php", true);
   hr.send(data);

然后,当您在 PHP 文件中获取数据时,它类似于:

 $data = $_POST['data'];
 $array = json_decode($data, true);

这一切都将告诉 php 将我们的数据转换为关联数组。然后可以将其作为关联数组进行操作。

于 2012-11-02T23:54:40.397 回答
1

简单地将数组转换为字符串

var data = [1,2,"hello"];
var data_str = data.join(","); 

然后将字符串转换为php中的数组:

$array = explode(",", $_REQUEST["data"]);
于 2012-11-03T00:06:18.697 回答
1

我实际上只是在做这个。

jQuery

var group_ids = $('.form-elements li').map(function(){
    return $(this).data('group-id')
}).get()

$.get('{{ url('group/update_order_by') }}', {group_ids: group_ids});

来自宁静的 Laravel 框架的 PHP

public function get_update_order_by()
{
    $group_ids = Input::get("group_ids");
    $group_count = count($group_ids);

    for ($i = 0; $i < $group_count; ++$i) {
        $group = Group::find($group_ids[$i] );
        $group->order_by = $i;
        $group->save();
    }

    return true;
}

原始 PHP(呃...)

    $group_ids = $_GET("group_ids");
    $group_count = count($group_ids);

    for ($i = 0; $i < $group_count; ++$i) {
        echo $group_ids[$i];
    }

    return true;
于 2012-11-02T23:53:45.237 回答
0

在 PHP 中,REQUEST期望数组以下列格式传递:

sendData1[]=first&sendData1[]=second&sendData1[]=third

这会自动转换为数组:

$sendData = $_REQUEST['sendData`];
echo $sendData[0];
echo $sendData[1];
于 2012-11-02T23:43:26.577 回答
0

首先,为了简单起见,创建一个包含要发送和解析的两个数组的数组:

var request = Array(dates, submissions);

其次,确保您以格式正确的 JSON 格式发送请求。此外,在测试时,我建议在您的回调中返回远程文件的输出,以查看您的数据是如何被解析的。我推荐以下实现,以及通过 POST 发送:

$.ajax({        
   type: "POST",
   url: "NAMEOFPHPFILE.php",
   contentType: "application/json; charset=utf-8",
   data: JSON.stringify(request),
   success: function(data) {
        alert(data);        
   }
}); 

在您的 PHP 文件中,您可以使用以下命令将上述请求的内容解析为一个数组:

<?php
// convert json data read from the input stream to an array
// *note* you MUST set the true flag in the json_decode() function, otherwise it will convert the data into an object
$request = json_decode(file_get_contents('php://input'), true);
var_dump($request);

您现在应该得到一个包含数组 var_dump 的警报(看起来很乱,我知道!)。

这应该足以让您入门,希望对您有所帮助!

于 2012-11-03T00:11:22.167 回答