[英]Sending file together with form data via ajax post in php
Here is what I'm trying to do.这就是我想要做的。
I'm uploading a file on the form and send the data via ajax.我在表单上上传一个文件并通过 ajax 发送数据。
Here is my code:这是我的代码:
<form enctype="multipart/form-data" name="addcert_form" id="addcert_form" method="post" >
<input type='hidden' id="staffid" name="staffid" value="<?php echo $staffid; ?>" />
<label for="certNumber"><?php echo("Certification Number"); ?></label>
<input type="text" class="form-control certNumber" id="certNumber" name="certNumber" >
<div class="form-group" style="margin-bottom: 0px;margin-left: 15px;">
<label for="file-upload"><?php echo('File Upload'); ?></label>
<input type='hidden' id="file-upload-hidden" name="file-upload-hidden" />
<input type="file" name="file-upload" id="file-upload" accept="application/pdf">
</div>
</form>
$(document).on('change', "#cert-upload", function() {
var ajaxurl = generalObj.ajax_url;
var form = $("#addcert_form");
var params = form.serializeArray();
var formData = new FormData();
var file_data = $('#file-upload').prop('files')[0];
$(params).each(function (index, element) {
formData.append(element.name, element.value);
});
formData.append('file-upload', file_data);
$.ajax({
url: ajaxurl + "rzvy_staff_ajax.php",
data: formData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function (res) {
if(res=="file-uploaded"){
swal.fire(generalObj.updated, generalObj.cert-upload_changed, "success");
//location.reload();
}
}
});
});
However, when I see the information, I'm only getting the file info.但是,当我看到信息时,我只得到文件信息。
I have tried doing $_POST, and $_FILES, however, I'm not getting the information from the form.我尝试过 $_POST 和 $_FILES,但是,我没有从表单中获取信息。 How can I get all of the information from the form when I upload the file?上传文件时如何从表单中获取所有信息?
Thank you, Kevin谢谢你,凯文
Here is what I found out.这是我发现的。 The data was coming over, I should have used both $_FILES and $_POST数据过来了,我应该同时使用 $_FILES 和 $_POST
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.