从jQuery

时间:2016-06-08 05:03:06

标签: php jquery arrays

我有这个jQuery代码添加到$ each循环中的数组:

new_edit = {};
new_edit['file_id'] = $file_id;
new_edit['file_realname'] = $new_file_realname;
new_edit['file_folder'] = $new_file_folder;
new_file_edits.push(JSON.stringify(new_edit));

jQuery new_file_edits数组输出

{"file_id":"1857","file_realname":"dddd[1].mp4","file_folder":"/"}, 
{"file_id":"1856","file_realname":"aaaa[1].jpg","file_folder":"/"},
{"file_id":"1855","file_realname":"ssss[1].jpg","file_folder":"/"}

,我试图将它发布到call.php,如下所示:

$.ajax({
    type: "POST",
    url: "/call.php",
    data: "edit_files="+ arrayVarHere,
    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        success: function(msg){
            alert(+msg);
        }
    });

在PHP文件call.php中我有这段代码:

if(isset($_POST['edit_files'])){
    $edit_array = $_POST['edit_files'];
    $file_array = array();
    foreach($edit_array as $key => $value){
        $file_array[$key] = $value;
    }
print_r($file_array);
    die();
}

但是我得到了这个错误,我无法弄清楚如何修复它,现在谷歌搜索了一段时间......

错误:

  

警告:第237行/home2/dddd/public_html/call.php中为foreach()提供的参数无效

第237行:foreach($edit_array as $key => $value){

非常感谢任何帮助!

-Morten

编辑1: 我将$edit_array = $_POST['edit_files'];更改为$edit_array = array($_POST['edit_files']);

现在输出:

{"file_id":"1857","file_realname":"dddd[1].mp4","file_folder":"/"}, 
{"file_id":"1856","file_realname":"aaaa[1].jpg","file_folder":"/"},
{"file_id":"1855","file_realname":"ssss[1].jpg","file_folder":"/"}

我如何使用foreach($edit_array as $key => $value){部分离开这里?

编辑2: 我构建我的arrayVarHere就像这样:

$.each( $selected_files_array, function( index, value ){
//i get $file_id, $new_file_realname and $new_file_folder with some code here
        new_edit = {};
        new_edit['file_id'] = $file_id;
        new_edit['file_realname'] = $new_file_realname;
        new_edit['file_folder'] = $new_file_folder;
        arrayVarHere.push(JSON.stringify(new_edit));
    });

5 个答案:

答案 0 :(得分:0)

像这样更改你的代码:

数据:" edit_files =" + arrayVarHere,

答案 1 :(得分:0)

在call.php

中按json_decode调用json数据
if(isset($_POST['edit_files'])){
$edit_array = json_decode($_POST['edit_files'],true); //return array 
$file_array = array();
foreach($edit_array as $key => $value){
    $file_array[$key] = $value;
}
print_r($file_array);
die();
}
你的ajax警报中的

应该是

 success: function(msg){
            alert(msg); //remove plus sign ,this can output `NaN`
        }

答案 2 :(得分:0)

检查 edit_array 是数组print_r($ edit_array);

检查 contentType 是否使用表单数据然后更正,否则使用json类型

如果是表格数据,请检查数据:edit_files []:arrayVarHere

答案 3 :(得分:0)

返回此数组

Array ( [0] => [{"file_id":"1857","file_realname":"aq53pop_460sv[1].mp4","file_folder":"/"},{"file_id":"1859","file_realname":"aKqbD8Q_460sv[1].mp4","file_folder":"/"},{"file_id":"1856","file_realname":"aDopymK_700b[1].jpg","file_folder":"/"}] ) 

如何循环数组并访问file_id值等?

答案 4 :(得分:0)

将数据传递给PHP的方式可能并不理想。以下是可能让您入门的内容:

PHP:call.php

    <?php

        $data0      = isset($_POST['data_0']) ? json_decode($_POST['data_0'], true) : null;
        $data1      = isset($_POST['data_1']) ? json_decode($_POST['data_1'], true) : null;
        $data2      = isset($_POST['data_2']) ? json_decode($_POST['data_2'], true) : null;

        $arrData    = array(
            'data0' => $data0,
            'data1' => $data1,
            'data2' => $data2,
        );

        foreach($arrData as $dataKey=>$dataObject){
            $tempFileID     = $dataObject->file_id;
            $tempFileName   = $dataObject->file_realname;
            $tempFileFolder = $dataObject->file_folder;

            // DO SOMETHING WITH THESE DATA AND THEN BUILD UP A RESPONSE FOR AJAX + SEND IT...

            // UNCOMMENT TO SEE THE DATA IN YOUR NETWORKS CONSOLE BUT 
            // IF YOU DO THIS (SINCE YOU EXPECT JSON RESPONSE) YOUR AJAX WILL RESPOND WITH AN ERROR...
            // var_dump($dataObject);
        }

        // RESPOND WITH THE SAME DATA YOU RECEIVED FROM AJAX: JUST FOR TESTING PURPOSES... 
        die( json_encode($arrData));

    ?>

JAVASCRIPT:为PHP构建数据

    <script type="text/javascript">
        var dataForPHP              = {};
        var iCount                  = 0;

        $.each( $selected_files_array, function( index, value ){
            var keyName                 = "data_" + iCount;
            new_edit                    = {};
            new_edit['file_id']         = $file_id;
            new_edit['file_realname']   = $new_file_realname;
            new_edit['file_folder']     = $new_file_folder;
            dataForPHP[keyName]         = new_edit;
            iCount++;
        });
    </script>

JAVASCRIPT:AJAX请求

    <script type="text/javascript">
        $.ajax({
            type        : "POST",
            url         : "/call.php",
            datatype    : "JSON",
            data        : dataForPHP,
            contentType: "application/x-www-form-urlencoded;charset=UTF-8",

            success: function(msg){
                if(msg){
                    alert("Ajax Succeeded");
                    console.log(msg);
                }
            }
        });
    </script>