我有一個簡單的文件上傳表單,只有當我從表單html文件中刪除ajax腳本時才能正常工作。如果沒有提交,我將被帶到一個空白頁面,顯示正確的錯誤或成功消息(它應該像驗證一樣)。文件上傳和PHP驗證僅適用於不使用ajax文件
如果我包含它,它會繞過我所有的參數並跳到最後一個錯誤。在那裏說你必須上傳一個文件才能繼續。
在我的ajax文件中是否存在缺少的東西?我無法弄清楚爲什麼它跳過我所有的驗證。
感謝您的幫助,您可以給
表HTML文件
<form enctype="multipart/form-data" id="anytimereg-form" class="shake" method="post" action="test-process-form.php'?>">
<div id="frm-filehandler">
<div id="file-drop" class="well">
<input type="file" name="upload" id="uplfile" class="inputfile" />
<label for="uplfile"><i class="fa fa-upload" aria-hidden="true"></i>Choose a file</label>
</div><!--end #file-drop-->
</div><!--end #frm-filehandler-->
<input type="hidden" name="action" value="upload"/>
<button id="submit" name="submit" type="submit" class="action-button btn-lg">Submit Registration</button>
</form>
測試process.php
<?php
$errors = array();
$data = array();
// A list of permitted file extensions
$allowed = array('png', 'jpg', 'pdf');
if(isset($_FILES['upload']) && $_FILES['upload']['error'] == 0){
$extension = pathinfo($_FILES['upload']['name'], PATHINFO_EXTENSION);
//Max Fiels Size
$maxsize = 2097152;
//File Size
$file_size = $_FILES['upload']['size'];
//The File Path
$file_path = '../../../../../upl_docs/';
//The File Name
$file_name = $_FILES['upload']['name'];
//Is the file type allowed
if(!in_array(strtolower($extension), $allowed)){
$errors['upload'] = 'File type not allowed';
}
//Does the file already exist
if(file_exists($file_path.$file_name)){
$errors['upload'] = $file_name.' That file already exists. Please select another or rename your file.';
}
//is the file size to big
if($file_size >= $maxsize){
$errors['upload'] = 'Your File is too large. File must be less than 2 megabytes.';
}
if(empty($errors)){
//We upload the file to outside of the root directory for security reasons
if(move_uploaded_file($_FILES['upload']['tmp_name'], $file_path.$file_name)){
$success['upload'] = $file_name.' Message: File has been uploaded';
$data['success'] = $success;
}else{
$errors['upload'] = 'Could not find the directory';
}
//$data['success'] = $success;
}//If empty of errors
else{
$data['success'] = false;
$data['errors'] = $errors;
}
}else{
$errors['upload'] = 'You must upload a File to continue';
$data['errors'] = $errors;
}
echo json_encode($data);
?>
阿賈克斯文件
// Set up an event listener for the contact form.
$(form).submit(function(event) {
$('.error-message').remove();
// Serialize the form data.
var formData = $(form).serialize();
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData,
dataType :'json',
encode:true
})
.done(function(data){
//Log the data into the console so that we can be sure what is happening
console.log(data);
//If we do have errors create the
if(!data.successmessage){
if(data.errors){
$(form).removeClass('success');
$('.submit-success').remove();
$(form).addClass('form-has-error'); // add the form-has-error-class
if(data.errors.upload){
$('#frm-filehandler').addClass('has-error'); // add the error class to show red input
$('#frm-filehandler').append('<div class="error-message"><p>' + data.errors.upload + '</p></div>');
// add the actual error message under our input
}
$('footer').prepend('<div class="error-message"><p>There are errors with your submission. Errors will be marked in red</p></div>');
}//end data errors
}//end no data successmessage
else if(data.successmessage){
//Remove the errors stuff
$('.error').remove();
$('.error-message').remove();
$(form).removeClass('form-has-error'); // add the form-has-error-class
$('.submit-success').remove();
//Add the success stuff
$(form).addClass('success');
$('footer').prepend('<div class="success-message"><p>' + data.successmessage + '</p></div>');
}//End if successmessage
})//end done function
.fail(function(data){
//If there is a failed submission lets log the errors
console.log(data);
});
//Stop the broweser from submitting the form
event.preventDefault();
});
});
[你看了在瀏覽器的開發者工具的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?你在網絡服務器上運行這個嗎?](http://jayblanchard.net/basics_of_jquery_ajax.html) –
感謝您的回覆, – bilcker
感謝您的回覆,沒有錯誤,我已經包含了jquery庫。是的,我正在服務器上運行,並且當我在開發人員工具中查看響應時,我所得到的僅僅是和響應上傳的對象:「您必須上傳文件才能繼續。」有沒有更好的方法來審查答覆? – bilcker