如何通过ajax传递文件数组数据以及如何在php文件中接收数据?

时间:2021-10-17 21:37:04

How to pass file array data by ajax and how to receive data by php file?

如何通过ajax传递文件数组数据以及如何通过php文件接收数据?

Please send me the example code of this issue. I have got data by FormData($(this)[0]) but fetch some problem. First time not got any data from text area (refresh page i.e. after page load first time). But second time I got (not refresh page).

请把这个问题的示例代码发给我。我有FormData的数据($(this)[0]),但是遇到了一些问题。第一次没有从文本区域获取任何数据(刷新页面,即第一次页面加载后)。但第二次我得到了(不刷新页面)。

I have made some code

我做了一些代码

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
</head>

<body>

    <script>        
        $(document).ready(function() {
            $("form#formD").submit(function(event){             
                event.preventDefault();
                var name = $("#name").val();
                var text = $("#text").val();
                var upload = $("#upload").val();
                $.ajax({
                    type: "POST",
                    url: "test.php",
                    data: {"name" : name, "" : text, "upload" : JSON.stringify(upload)},
                    dataType: 'json',
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,                     
                    beforeSend: function(){ $("#send").val('Sending...'); },
                    success: function( html ){  
                        alert( html );
                        $("#send").val('send');
                    }
                });             
                return false;
            });
        });     
    </script>
    <form id="formD" action="" method="POST" enctype="multipart/form-data" accept-charset="utf-8">
    <label>File name</label>: <input type="text" name="name" id="name" required /><br /><br />
    <label>File Description</label>: <textarea id="text"></textarea><br /><br />
    <label>Select File</label>: <input type="file" name="upload[]" id="upload[]" required /><br />
    <input type="submit" value="send" id="send" />
    </form>
</body>
</html>

1 个解决方案

#1


1  

In the PHP file, you would use the super global $_POST. This is an array of all data passed via the POST command.

在PHP文件中,您将使用超级全局$ _POST。这是通过POST命令传递的所有数据的数组。

To iterate through it, would be like

迭代它,就像

foreach($_POST as $sPostKey => $sPostValue) {

    // do stuff here

}

Remember to use json_decode to turn that JSON into something usable within PHP.

记得使用json_decode将JSON转换为PHP中可用的东西。

#1


1  

In the PHP file, you would use the super global $_POST. This is an array of all data passed via the POST command.

在PHP文件中,您将使用超级全局$ _POST。这是通过POST命令传递的所有数据的数组。

To iterate through it, would be like

迭代它,就像

foreach($_POST as $sPostKey => $sPostValue) {

    // do stuff here

}

Remember to use json_decode to turn that JSON into something usable within PHP.

记得使用json_decode将JSON转换为PHP中可用的东西。