Форма отправляется без изображения - PullRequest
0 голосов
/ 17 октября 2019

без вставки изображения пользователь может отправить форму с моим кодом. Изображение пустое, предупреждение отображается только тогда, когда я нажимаю кнопку загрузки изображения, и я хочу отображать это сообщение, когда пользователь отправляет форму без изображения.

without inserting the image user is being able to submit the form with my code. Image is empty alert is only displayed only when i click the upload image button, and i want to display that message when the user submit the form without image.





<form id="entrance" role="form"  action="saveentrance.php" method="POST" enctype="multipart/form-data">
  <div class="container my-1 text-white" style="background-color: #070707;">        
    <div class="form-group form-inline form-row">
      <label class=" control-label col-sm-2" for="entrance_voucher_no">Voucher no</label>
      <div class="col-sm-2">
        <input type="text" class="form-control " name="entrance_voucher_no" id="entrance_voucher_no" placeholder=" Voucher no" required>
      </div>
    </div>

    <div class="form-group form-inline form-row">
      <label class="control-label col-sm-2" for="photo">Your photo</label>
      <div class="col-sm-2">
        <input type="hidden" name="MAX_FILE_SIZE" id="maxsize" value="800000">
        <input type="button" class="btn-secondary" style="display:block;width:200px; height:38px;" onclick="document.getElementById('photo').click()" value="Upload Photo"  onchange="return Validate()" required>
        <input type="file" style="display: none;" name="pp_photo" id="photo" value="Upload Photo" accept='.gif,.jpeg,.jpg,.bmp,.png,' onchange="return Validate()" required />
      </div>
    </div>

    <div class="form-group mb-0 pb-3"> 
      <div class="col-sm-2">
        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
  </div>
  </form>

  <script type="text/javascript">
    function Validate()
    {
      var image =document.getElementById("photo").value;
      if(image=='')
      {
        alert("Upload your pp sized photo.");
      }else{
        var checkimg = image.toLowerCase();
              if (!checkimg.match(/(\.jpg|\.png|\.JPG|\.PNG|\.jpeg|\.JPEG|\.bmp|\.BMP)$/)){ // validation of file extension using regular expression before file upload
                document.getElementById("photo").focus();

                alert("This image is not a valid image type. Please use a image of known types gif, jpg, jpeg, bmp or png.");
              }
              var img = document.getElementById("photo"); 
                if(img.files[0].size >  800000)  // validation according to file size
                {
                  alert("The image size should be less than 100 KB");
                  return false;
                }
                return true;
              }
            }
          </script>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...