我正在尝试使用 ajax 将文件和文本插入数据库,但它不起作用。主要问题是文件未插入数据库这是我的代码
<form id="descriptionsubmit" enctype = "multipart/form-data">
<textarea class="form-control" id="textarea-description" placeholder="Write Something"></textarea>
<input type="file" name="upload-pic" id="upload-pic" class="inputfile" >
<button type="submit" class="update-btn">Next</button>
</form>
Ajax 同样的警报(数据)没有显示任何内容
$(function(){
$('form#descriptionsubmit').on('submit', function (e) {
e.preventDefault();
var file_data = $('#upload-pic').prop('files')[0];
var formData = new FormData(this);
$.ajax({
method: "POST",
url : "AJAXSubmitClientData.php",
data :formData,
dataType : "html",
contentType: false,
enctype: 'multipart/form-data',
cache: false,
processData:false,
success:function(data)
{
alert(data);
}
});
PHP代码这里插入了描述但没有插入文件
if(isset($_POST['formData']))
{
$file = array(
"name" => $_FILES['form_data']['name'],
"tmp_name" => $_FILES['form_data']['tmp_name']
);
print_r($file);
$fileinserted = $objMaster->imageinsert($file);
$dataDescription = array(
"description" => $_POST['description'],
"projectProfile" => $fileinserted,
);
echo $insertdata = $objMaster->updateJobPostTable($dataDescription,$_SESSION['lastinsertid']);
echo 1;
}
图像插入函数
public function imageinsert($file, $path = "") {
$fname = "";
$uploadpath = "";
if ($path == "") {
$uploadpath = $this->upload1;
} else {
$uploadpath = $this->upload1 . $path . "/";
}
$current_timestamp = $this->timeStamp();
$filename = basename($file['name']);
$newname = $current_timestamp . $filename;
if ((move_uploaded_file($file['tmp_name'], $uploadpath . $newname))) {
$fname = $newname;
}
return $fname;
}
任何帮助表示赞赏
临摹微笑