簡體   English   中英

我如何檢查輸入數組字段類型為空的文件是否未上傳

[英]How can i check if input array filed type file empty doesn't upload

在數據庫中上傳數據之前,如何檢查數組輸入類型文件是否為空。 如果有一個空白,則顯示用戶可以理解的任何內容,而不更新數據。

這是示例代碼html

 <h3>Extra profile information</h3>
 <table class="form-table">
     <tr>
     <th><label for="Upload">Upload</label></th>
     <td>
        <form action="" method="post">
        <input name="profile_photo[]" type="file"  value="" />
        <input name="profile_photo[]" type="file"  value="" />
        <input name="profile_photo[]" type="file"  value="" />
        <br/>
    </form>
     </td>
 </table>

我曾經使用$ x = $ _ GET ['profile_photo']; 並回顯$ x; 在上載到數據庫之前,它返回空或null。

if (!empty($_FILES) && is_array($_FILES)) {

  // you have files
}
$x=$_FILES['profile_photo']['name']; 

使用此代碼

只需在發布數據上運行for循環並驗證

$error = "";
$emptyFiles="";
$i=0;
foreach ($_FILES['profile_photo'] as $name) {
   if (isset($name['name']) && $name['name']!="") {
       $error.="";
   } else {
       $error.= "yes";
       $emptyFiles.=$_FILES['profile_photo'][$i];
   }
    $i++;
}

然后使用

$ error和$ emptyFiles字符串獲取哪些是空字段,哪些不是。

您還可以在提交之前通過javascript驗證輸入

HTML:

<form action="" method="post" onsubmit="return validateInputs();">
    <input name="profile_photo[]" type="file"  value="" />
    <input name="profile_photo[]" type="file"  value="" />
    <input name="profile_photo[]" type="file"  value="" />
    <input type="submit" />
</form>

JavaScript的:

validateInputs = function() {    
    inputs = document.getElementsByName('profile_photo[]');
    for (var ind in inputs){
        if (inputs[ind].value=="") {
            alert("validation failed");
            return false;
        }
    }         
}

在javascript / jquery中

var file = $(".profile_photo").map(function(){return $(this).val();}).get();


for(var i=0; i<file.length; i++){
   if( file[i] == ""){
     alert("Please select file");
     return false;
   }
}

在PHP中

if( $_FILES['profile_photo']['error'] != 0 ){
   // code to handle if there is no file selected
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM