文件类型数据未进入处理页面 [英] File type data not getting in the process page
问题描述
我正在动态添加正在运行的文件类型,但是当我单击提交"时,我仅获得过程页面中的第一个文件名.如何获取进程页面中的所有文件名?我尝试了var_dump($_FILES)
进行检查,但它仅显示第一个文件名.
I am adding the file type dynamically which is working but when I click on submit then I am getting only the first filename in the process page. how to get all the file name in process page? I tried var_dump($_FILES)
to check but it is displaying only the first file name.
然后我注意到,如果我从名称(name="workpic[]' + numberIncr + '"
)中删除numberIncr,则该代码有效,但我的验证无效.
then I notice that if I remove numberIncr from the name ( name="workpic[]' + numberIncr + '"
) then it's working but my validation not working.
Process.php
if(isset($_FILES)){
echo"<pre>";
//var_dump($_FILES) ;
print_r($_FILES['workpic']['name']);
foreach($_FILES['workpic']['name'] as $key=>$val){
$fileName = basename($_FILES['workpic']['name'][$key]);
print_r($fileName);
}
}
$(document).ready(function() {
var maxField = 10; //Input fields increment limitation
var x = 1; //Initial field counter is 1
//var count = 2;
var numberIncr = 1; // used to increment the name for the inputs
// var addrm = '';
//Once add button is clicked
$(document).on('click', '#clicktoadd', function() {
//Check maximum number of input fields
if (x < maxField) {
$(".medication_info").append(' <input type="file" name="workpic[]' + numberIncr + '" class="dynamicVal"><br />');
x++; //Increment field counter
numberIncr++;
}
// count++;
});
});
$('#register').on('submit', function(event) {
event.preventDefault();
// adding rules for inputs with class 'comment'
$('.dynamicVal').each(function() {
$(this).rules("add", {
required: true
})
});
// test if form is valid
if ($('#register').validate().form()) {
var formData = new FormData(this);
$.ajax({
//url:"process.php",
url: "process2.php",
type: "POST",
dataType: "json",
data: formData,
contentType: false,
cache: false,
processData: false,
success: function(data) {
alert("success");
},
}); // AJAX Get Jquery statment
}
//alert('hellow');
});
$('#register').validate({
errorPlacement: function(error, element) {
if (element.is("select")) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
<div id="clicktoadd"><a href="javascript:void(0);" class="btn btn-bg">Add More</a></div>
<form action="#" method="post" id="register" name="register" enctype="multipart/form-data">
<div class="row">
<div class="medication_info">
<input type="file" name="workpic[]" class="dynamicVal"><br />
</div>
</div>
<input type="submit" name="send" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>
谁能帮我解决这个问题?
Can anyone help me out with this issue?
我得到了这个输出,但是我添加了3张图像,并且只显示了一张.
I am getting this output but I added 3 images and it's displaying only one.
Array
(
[0] =>bTml75QAfHo-unsplash.jpg
)
您能帮我解决这个问题吗?
Would you help me out in this issue?
推荐答案
您不应将numberIncr
放在name=workpic[]
之后.使用以[]
结尾的名称将自动使其成为PHP中的数组.因此,只需:
You shouldn't put numberIncr
after name=workpic[]
. Using a name ending in []
will automatically make it an array in PHP. So just do:
$(".medication_info").append(' <input type="file" name="workpic[]" class="dynamicVal"><br />');
如果您需要提供特定的索引,而不是让它们从0
开始自动递增,则应该将索引放在内部,而不是紧随其后.
If you need to give specific indexes, rather than letting them automatically increment from 0
, you should put the index inside the []
rather than after it.
$(".medication_info").append(' <input type="file" name="workpic[' + numberIncr + ']" class="dynamicVal"><br />');
这篇关于文件类型数据未进入处理页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!