我有一个简单的文件上传表单,只有当我从表单html文件中删除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);
?>
Ajax文件
// 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();
});
});
答案 0 :(得分:1)
您无法像处理文本数据一样发布文件。您需要使用xhr请求发布文件。
了解更多信息:
答案 1 :(得分:-1)
老实说,你发布的AJAX代码非常糟糕......显示/隐藏简单的成功或错误消息是多么糟糕和错综复杂的方式。我的意思是,如果表单成功验证,则需要六行代码来隐藏错误消息...
我会重新编程所有这些(包括PHP),使其更简单。不应该采用所有代码来制作简单的AJAX表单。