如果只有文本字段要更新,则此代码有效。但是当我尝试上传任何文件时,无法上传。如果我对下面的jQuery进行评论,那么它可以正常工作但不会在模型对话框中显示错误。
PHP脚本:
<?php
include_once 'db/Common.php';
include_once 'db/CustomerDB.php';
include_once 'db/CustomerDetails.php';
$cust_id= $_POST ['hidCustId'];
function IsNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}
$uploadSuccess=false;
$files = array_fill(0,8,NULL);
for ($i=1;$i<=8;$i++){
if(isset($_FILES['file'.$i])){
$errors= array();
$file_name = $_FILES['file'.$i]['name'];
$file_size =$_FILES['file'.$i]['size'];
$file_tmp =$_FILES['file'.$i]['tmp_name'];
$file_type=$_FILES['file'.$i]['type'];
$file_ext=strtolower(end(explode('.',$_FILES['file'.$i]['name'])));
$files[$i-1] = $file_name;
$expensions= array("jpeg","jpg","png","pdf","docs","doc","exe", "pages");
if(in_array($file_ext,$expensions)=== true){
//$errors['file'.$i]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 10097152){
$errors[]='File size must be bellow 10 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uplodedfile/".$file_name);
$uploadSuccess=true;
}else{
print_r($errors);
}
}
}
$customerDB = new CustomerDB ();
$customers = new CustomerDetails ();
$customerArray = $customerDB->selectAllCustomers($cust_id, null, null, null, null);
$customer = $customerArray[0];
$files_old = $customer->get_custFiles();
for($i=0; $i<=7; $i++){
if(IsNullOrEmptyString($files[$i])){
if(!IsNullOrEmptyString($files_old[$i])){
$files[$i] = $files_old[$i];
}
}
}
$customerDB->updateFiles($cust_id,$files);
if ($uploadSuccess) {
$msgs = "Files updated successfully";
} else {
$errorMsg = "Error while updating Files details.";
}
?>
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]> <html class="lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="lt-ie9" lang="en"> <![endif]-->
<!--[if gt IE 8]><!-->
<html lang="en">
<!--<![endif]-->
<head>
<meta charset="utf-8">
<!-- Viewport Metatag -->
<?php $common = new Common(); echo $common->get_include_contents('include/css.php')?>
</head>
<body>
<?php
if (! empty ( $msgs )) {
?>
<div class="mws-form-message success">
This is a success message
<ol>
<li><?php echo $msgs; ?></li>
<li>Close this dialog box to continue.</li>
</ol>
</div>
<?php }elseif ($errorMsg){ ?>
<div class="mws-form-message error">
This is an error message
<ul>
<li><?php echo $errorMsg; ?></li>
</ul>
</div>
<?php }?>
使用Javascript:
$(document).on('submit', '#formEditFiles', function(event)
{
event.preventDefault();
var data = $(this).serialize();
$.ajax({
type : 'POST',
url : 'handleFilesEvent.php',
data : data,
success : function(response)
{
$(".quickNote").html(response);
}
});
return false;
});