使用ajax将JavaScript数组传输到PHP数组?

时间:2012-11-02 23:33:36

标签: php javascript ajax

我正在尝试将数据列表从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不起作用?数组显示为空......

5 个答案:

答案 0 :(得分: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将我们的数据转换为一个辅助数组。然后可以将其作为辅助阵列进行操作。

答案 1 :(得分: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;

答案 2 :(得分:1)

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

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

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

$array = explode(",", $_REQUEST["data"]);

答案 3 :(得分:0)

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

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

这会自动转换为数组:

$sendData = $_REQUEST['sendData`];
echo $sendData[0];
echo $sendData[1];

答案 4 :(得分: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的警报(看起来很乱,我知道!)。

这应该足以让你开始,希望它有所帮助!