我只是想把用户的图像上传的形式与识别号码。问题是,如果用户在没有填写任何内容的情况下按下submit,它会根据脚本显示一个警告,但随后会重定向到提供的POST链接。它不应该是这样的,我想阻止POST操作,如果有任何验证问题。我也没有看到这个片段的任何错误/日志错误。
这是我的代码-
<div class="row">
<div class="col-12 col-md-12 col-sm-6">
<!-- Attendacne Card Info Fields-->
<div class="card">
<div class="card-body">
<!--<h4 class="card-title mb-5">DSR Information Fields</h4>-->
<form action="./collect-dsrs-attendance-confirmation.php" method="POST" name="page331" enctype="multipart/form-data" onsubmit="return validate();">
<div class="form-body">
<div class="row">
<input type="text" class="form-control" id="lat" name="latitude" style="display:none;" value="">
<input type="text" class="form-control" id="long" name="longitude" style="display:none;" value="">
<!--Back Button For Each Page-->
<div class="col-md-12 mb-5 d-grid gap-2 d-md-flex justify-content-md-end">
<div class="form-actions text-start mt-3">
<a type="button" href="./index.php" class="btn btn-danger ps-4 pe-4"
id="backhome" style="background-color: rgb(255, 0, 0);">Back Home</a>
</div>
</div>
<div class="col-lg-12 col-md-12">
<div class="form-group mb-3">
<label class="form-label text-dark" for="dsrELNumber">ইএল নম্বর</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">+880</span>
</div>
<input class="form-control" id="dsrELNumber" type="text" name="el_number" id="elNumber"
placeholder="Enter EL Number" data-maxlength="10" data-minlength="10"
oninput="this.value=this.value.slice(0,this.dataset.maxlength)"
value="<?php if(!empty($_POST['dsr_el_number'])) echo $_POST['el_number']; ?>" required>
</div>
<div id="docImage">
<!--<video id="video" width="640" height="480" autoplay style="display:none;"></video>-->
<label class="custom-file-label text-primary mt-3" for="file-input">Take Selfie</label>
<input type="file" class="mt-4 form-control" id="captureDocument" name="user_img" accept="image/*" capture="camera" required>
<button class="btn text-white mt-4 mb-4 d-block ps-4 pe-4" type="submit" style="background-color: rgb(255, 0, 0);" id="btnTakeDocument">Submit</button>
</div>
<!--To show successfull message-->
<div id="message"></div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<!-- Attendacne Card Info Fields-->
</div>
</div>
<script>
function validate()
{
var valid = true;
var file_name = "";
var file_type = "";
var file_size = "";
var error_msg = "";
var valid_size = 3*1000*1000;
var display_error = document.getElementById('file_error');
var file = document.getElementById("user_img");
if(file.files.length != 0)
{
file_name = file.files[0].name;
file_size = file.files[0].size;
file_type = file.files[0].type;
if(file_type!="image/png" && file_type!="image/jpeg" && file_type!="image/gif")
{
valid = false;
error_msg = error_msg + "\n* Only 'PNG', 'JPG/JPEG' and 'GIF' file type supported.";
}
if(file_size > valid_size)
{
valid = false;
error_msg = error_msg + "\n* Filesize should be upto 3MB.";
}
}
else
{
valid = false;
error_msg = error_msg + "\n* Please select any image file.";
}
if(valid==true)
{
alert("File pass all validation. Now it is ready to send.");
/*Write ajax code here to send file to the server.*/
return true;
}
else
{
display_error.innerHTML = error_msg;
return false;
}
return valid;
}
</script>
1条答案
按热度按时间z2acfund1#
因为你发布的是一个php文件,你可以在服务器端验证之前重定向回那个页面。
或者,您可以将
onSubmit
迁移到jQuery/ AJAX 。它将允许您向用户显示错误/成功,而无需重新加载页面。