Javascript 如何在通过onChange()检查图像文件后使用jquery提交表单?
这是我的表格-Javascript 如何在通过onChange()检查图像文件后使用jquery提交表单?,javascript,php,jquery,forms,Javascript,Php,Jquery,Forms,这是我的表格- <form id="imgUpIcn" class="imgUpIcn" enctype="multipart/form-data" method="post" action="myfile.php"> <div id="upload-file-container"> <input type="file" name="uploadedImagefile" id="myImageFile" onchange="photoUpInit(event,thi
<form id="imgUpIcn" class="imgUpIcn" enctype="multipart/form-data" method="post" action="myfile.php">
<div id="upload-file-container">
<input type="file" name="uploadedImagefile" id="myImageFile" onchange="photoUpInit(event,this,'loadingImg','cropPhLayer')" />
</div>
</form>
现在,当执行表单操作时,我的文件将上载到服务器上,但我希望从php文件获得回调,而不是重定向到php页面
这是我的php文件-
<?php
if (isset($_FILES['uploadedImagefile']['tmp_name']))
{
$target_path = "../uploads/";
$target_path = $target_path . "tmp";
if(move_uploaded_file($_FILES['uploadedImagefile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedImagefile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}
考虑到是php文件上传了您的文件,您应该将php头重定向回表单页面,并显示成功或失败的消息。大概是这样的:
<?php
if (isset($_FILES['uploadedImagefile']['tmp_name']))
{
$target_path = "../uploads/";
$target_path = $target_path . "tmp";
if(move_uploaded_file($_FILES['uploadedImagefile']['tmp_name'], $target_path)) {
$message = "The file ". basename( $_FILES['uploadedImagefile']['name']).
" has been uploaded";
} else{
$message = "There was an error uploading the file, please try again!";
}
header('Location: back/to/your/form/file?message=' . $message);
}
我没有类似问题的提交按钮。我想在Ajax成功函数中获得结果。然后需要使用另一种方法,通过Ajax将图像发送到php脚本,称为ajaxupload。谷歌它,因为我们不允许提供图书馆的答案。
$('#imgUpIcn').on('submit', (function(e) {
e.preventDefault();
$.ajax({
type : "POST",
url : $(this).attr('action'),
data : $(this).serialize(),
success : function(data) {
$("#myModal").dialog("close");
},
error : function(data) {
console.log("error");
console.log(data);
}
});
}));
<?php
if (isset($_FILES['uploadedImagefile']['tmp_name']))
{
$target_path = "../uploads/";
$target_path = $target_path . "tmp";
if(move_uploaded_file($_FILES['uploadedImagefile']['tmp_name'], $target_path)) {
$message = "The file ". basename( $_FILES['uploadedImagefile']['name']).
" has been uploaded";
} else{
$message = "There was an error uploading the file, please try again!";
}
header('Location: back/to/your/form/file?message=' . $message);
}