嘿,我正在尝试向 mysql 数据库提交表单。不过,该表单位于模式内部。然后,数据通过 ajax 发送到 ajax 处理程序,并运行 php 函数。每次在处理程序中写入“文件”一词时,我都会收到错误。这是我的表单、ajax 和处理程序。
表单输入:
<div class="form-group">
<label>Image</label>
<input type="file" class="addInput" name="image" id="newImage" placeholder="">
</div>
JavaScript/Ajax
var name = $('#newName').val();
var description = $('#newDescription').val();
var notes = $('#newNotes').val();
var status = ($('#newStatus').is(":checked") ? 'active' : '');
var slug = $('#newSlug').val();
var start_date = $('#newStartDate').val();
var end_date = $('#newEndDate').val();
var image = $('#newImage').val();
let data = {
action: 'NewEventExhibition',
name: name,
description: description,
notes: notes,
status: status,
slug: slug,
start_date: start_date,
end_date: end_date,
image: image,
event_code: '<?=$code?>'
};
console.log(data);
$.ajax({
url: '/modules/ajax/ajax_handler.php',
type: 'POST',
data: data,
success: function(response) {
console.log(response);
},
fail: function(response) {
console.log(response);
}
})
处理程序
$added = $image = $filename = $imagefile = $imagefilesuccess1 = $imagefilewarning1 = NULL;
if(!empty($_POST['image'])){
$filename=($_SERVER['DOCUMENT_ROOT']."/assets/images/" . $_FILES["file"]["name"]);
$imagefile=($_FILES["file"]["name"]);
$path="/assets/images/".$imagefile."";
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "PNG");
$tmp = explode(".", $imagefile);
我只将图像代码包含在 ajax 处理程序中。$image底部的变量是我用来上传的变量。
慕后森