[英]Uploading both data and files in one form using POST method, AJAX and jQuery (Error)
I have a problem. 我有个问题。
formData.append("id_element",1)
doesn`t add the field into formData. formData.append("id_element",1)
不会将字段添加到formData中。 I have found a lot of solves but in my case it doesn`t work right. 我找到了很多解决方法,但就我而言,它无法正常工作。
PHP code here: 此处的PHP代码:
var_dump($_FILES);
$("#hw-upload_image-form").submit(function(e) { e.preventDefault(); var formData = new FormData(this); formData.append("id_element",1); // doesn`t work here $.ajax({ type:"POST", processData: false, contentType: false, cache: false, url:$(this).prop('action'), data:formData, success:function (data) { console.log(data); // show returned data from php } }); });
<form action="action.php" method="post" enctype="multipart/form-data" id="hw-upload_image-form"> <input type="file" name="hwImage"> <!-- hw = homework (just for you :) --> </form>
Result (from the console) 结果(从控制台)
array(1) {
["hwImage"]=>
array(5) {
["name"]=>
string(70) "73b38ef5d1f5849ea800c18990acde94_ce_1920x1200x0x0_cropped_800x427.jpeg"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(36) "D:\OSPanel\userdata\temp\php9F48.tmp"
["error"]=>
int(0)
["size"]=>
int(68411)
}
}
I have already undestood my mistake. 我已经知道我的错误了。
$_FILES
will show me only file data, but if I want to see id_element
I have to use $_POST
. $_FILES
仅显示文件数据,但是如果我想查看id_element
,则必须使用$_POST
。
The result is: 结果是:
array(1) {
["id_element"]=>
string(1) "1"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.