【发布时间】:2018-12-19 00:06:45
【问题描述】:
我对使用 FormData 对象还很陌生,并且仍在尝试熟悉它的工作原理。所以,我设法将一个视频文件发布到一个 php 脚本,该脚本处理将视频文件上传/移动到一个文件夹。但是,我无法传递或发布其他表单输入信息,例如在表单的文本字段中键入的文本。
我的目标是将所有表单数据(输入字段上的视频文件和文本)传递/发布到 php 脚本。我尝试var_dump$_POST 数组查看除了$_FILES['file'] 之外是否还有其他数据。
我能够获取文件输入,但不能获取文本输入。我想一键发送所有的表单数据。
以下是我从var_dump() -ing $_POST 和$_FILES 得到的信息
array(0) {
}
array(1) {
["file"]=>
array(5) {
["name"]=>
string(48) "How to Create App Shortcut on Ubuntu Desktop.mp4"
["type"]=>
string(9) "video/mp4"
["tmp_name"]=>
string(14) "/tmp/phprG6kRC"
["error"]=>
int(0)
["size"]=>
int(10522522)
}
}
它为var_dump($_POST) 显示array(0){}
index.php
<form id="myForm" method="POST" enctype="multipart/form-data">
<label>Lastname: </label> <input type="text" name="lastname" id="lastname" required/><br/>
<label>Firstname: </label> <input type="text" name="firstname" id="firstname" required/><br/>
<label>Middlename: </label> <input type="text" name="middlename" id="middlename" required/><br/>
<label class="modal_label" id="modalLbl_browseVideo">
Select Video
<input type="file" name="file" id="file_input" value="Select Video" accept="video/*"/>
</label>
<button name="myButton" id="myButton">
Upload
</button>
<br/>
<label id = "errorLabel"></label>
</form>
<script src="js/jquery-3.3.1.js"></script>
<script src="js/index.js"></script>
index.js
$('#myButton').click(function(){
upload();
});
function upload() {
var formData = new FormData($('#myForm')[0]); //initialized with myForm
$.ajax({
url: 'upload_video.php',
type: 'POST',
data: formData,
cache: false;
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (data) {
console.log(data);
alert(data);
},
error: function (x, e) {
if (x.status == 0) {
alert('You are offline!!\n Please Check Your Network.');
} else if (x.status == 404) {
alert('Requested URL not found.');
} else if (x.status == 500) {
alert('Internal Server Error.');
} else if (e == 'parsererror') {
alert('Error.\nParsing JSON Request failed.');
} else if (e == 'timeout') {
alert('Request Time out.');
} else {
alert('Unknown Error.\n' + x.responseText);
}
}
});
}
上传.php
var_dump($_POST);
var_dump($_FILES);
我理解它的方式是var formData = new FormData($('#myForm')[0]); 使用表单具有并包含在formData 变量中的所有数据进行初始化。但它只是在var_dump() 上获取视频文件输入
为什么不能显示已发布的其他表单数据值,例如姓氏、名字和中间名?
如果有任何建议,我将不胜感激。
谢谢。
【问题讨论】:
标签: javascript php jquery form-data