這是我的形式 -如何通過onChange()檢查圖像文件後提交與jQuery的表單?
<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>
這是我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";
}
}
現在,當執行表單動作我的文件得到上載到服務器上,但我想從一開始回調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!";
}
}
我用它來獲取回調,但它不是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);
}
});
}));
[表單提交與AJAX傳遞表單數據到PHP沒有頁面刷新](http://stackoverflow.com/questions/16616250/form-submit-with-ajax-passing-form-data-to-php -without-page-refresh) – Ryan
我沒有像這個問題那樣的提交按鈕。 – Mihir