Javascript 在显示sweet alert后,如何在php中使用ajax上传文件?
我目前在将文件输入(图像)插入数据库时遇到问题,但当我尝试将其插入数据库时,它返回空值 这是我的剧本Javascript 在显示sweet alert后,如何在php中使用ajax上传文件?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我目前在将文件输入(图像)插入数据库时遇到问题,但当我尝试将其插入数据库时,它返回空值 这是我的剧本 <script> $("#insertform").on('submit',(function(e) { /* $('#submit').toggleClass('active'); document.getElementById('submit').disabled = 'disabled'; */
<script>
$("#insertform").on('submit',(function(e) {
/* $('#submit').toggleClass('active');
document.getElementById('submit').disabled = 'disabled'; */
var data=$('#insertform').serialize();
e.preventDefault();
swal({
title: "Are you sure",
text: "Do you want to save hotel data",
type: "info",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true, },
function(){
setTimeout(function(){
$.ajax({
url: "hotels/insert_Hotel.php",
type: "POST",
data: new FormData(this),
async: false,
success: function(data){
swal("Saved! your hotel data has been saved");
location.reload();
},
contentType: false,
cache: false,
processData:false,
error: function(){
alert('error handing here');
}
});
}, 2000);
});
}));
</script>
$(“#insertform”)。关于('提交',(函数(e){
/*$('#submit')。toggleClass('active');
document.getElementById('submit').disabled='disabled'*/
var data=$('#insertform').serialize();
e、 预防默认值();
swal({
标题:“你确定吗”,
文本:“是否要保存酒店数据”,
输入:“信息”,
showCancelButton:true,
CloseOnConfig:false,
showLoaderOnConfirm:true,},
函数(){
setTimeout(函数(){
$.ajax({
url:“hotels/insert_Hotel.php”,
类型:“POST”,
数据:新表单数据(本),
async:false,
成功:功能(数据){
swal(“已保存!您的酒店数据已保存”);
location.reload();
},
contentType:false,
cache:false,
processData:false,
错误:函数(){
警报(“此处处理错误”);
}
});
}, 2000);
});
}));
这是我的php文件
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['logo']['tmp_name'])) {
$randomvalue=rand(1,999999999);
$date=date("Y-m-d H:i:s a");
$sourcePath = $_FILES['logo']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT']."/hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
$dbpath="../../hotelLogos/".$randomvalue.$date.$_FILES['logo']['name'];
move_uploaded_file($sourcePath,$targetPath);
}
else{
echo 'file not uploaded';
}
}
?>
它不会将文件上载到文件夹中,也不会向表中插入值 newformdata(this)
此处this
不是表单,您需要在ajax中的newformdata(form)
中传递表单:
data: new FormData($('#insertform')[0]), // <---change to this.
data:newformdata($('#insertform')[0]),//newformdata(this)
此处this
不是表单,而是需要在ajax中的newformdata(form)
中传递表单:
data: new FormData($('#insertform')[0]), // <---change to this.
data:newformdata($('#insertform')[0]),//它返回空值
除非出现错误,否则不返回任何值。。?我建议您阅读这个问题:在使用ajax保存图像时,我看到了很多错误和问题。我建议您使用fileupload.js()通过js上传文件。最好的。它返回空值
除非有错误,否则不返回任何值。。?我建议您阅读这个问题:在使用ajax保存图像时,我看到了很多错误和问题。我建议您使用fileupload.js()通过js上传文件。这将导致一个错误,FormData
采用表单HTMLElement,而不是jQuery对象。你的意思是FormData($('#insertform')[0])
?@RoryMcCrossan第一个建议是正确的,但是这个指的是那里的表单吗?@lahiru当你想将jQuery对象转换为domhtmlelement时,正如Rory在第一个注释中所建议的那样。@lahiru你可以像newformdata一样放置本机选择器(document.getElementById('#insertform'))
这会导致错误,FormData
采用表单HtmleElement,而不是jQuery对象。您的意思是FormData($('#insertform')[0])
?@RoryMcCrossan第一个建议是正确的,但是这是指那里的表单吗?@lahiru,当您想将jQuery对象转换为domHtmlement时,正如Rory在第一条评论中所建议的那样。@lahiru您可以像newFormData(document.getElementById('.\35; insertform'))一样放置本机选择器。