[英]How to submit form with jquery after checking image file through onChange()?
This is my form - 这是我的表格-
<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>
This is my photoUpInit()
function - 这是我的
photoUpInit()
函数-
function photoUpInit(evt, obj, loaderID, cropLBID) {
var ext = obj.value.split('.').pop().toLowerCase(), vStat = true;
if (window.File && window.FileReader && window.FileList) {
var e = evt || window.event, files = e.target.files, fSize = Math.round((parseInt(files[0].size) / 2048) * 100) / 100;
if (fSize > 2048)
vStat = false;
}
if (ext != 'png' && ext != 'jpg' && ext != 'jpeg' && ext != 'gif')
vStat = false;
if (vStat) {
document.getElementById('uplErr').style.display = "none";
document.getElementById(loaderID).style.display = 'block';
//Submitting form here
obj.form.submit();
} else {
document.getElementById(loaderID).style.display = 'none';
document.getElementById('uplErr').style.display = "block";
}
}
Now when the form action is performed my file is getting uploaded on the server but I want to get callback from the php file instead I am redirected to the php page. 现在,当执行表单操作时,我的文件已上传到服务器上,但我想从php文件中获取回调,而不是将我重定向到php页面。
Here is my php file- 这是我的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!";
}
}
I already tried using this to get callback but its not working- 我已经尝试过使用它来获取回调,但是它不起作用-
$('#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);
}
});
}));
Considering that it's the php file that uploads your file, you should do a php header redirect back to the form page with a message on both success or failure. 考虑到上传文件的是php文件,您应该将php标头重定向回表单页面,并显示成功或失败消息。 Something like this:
像这样:
<?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);
}
In the end this should upload the file and go back to your form page which in my oppinion should also be a php file so that you can interpret the $_GET variable in the link. 最后,这应该上传文件并返回您的表单页面,在我看来,它也是一个php文件,以便您可以解释链接中的$ _GET变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.