简体   繁体   English

用PHP循环文件上传表单

[英]looping file upload form with PHP

im currently using jquery booklet Moleskine Notebook with jQuery Booklet to make a logbook with some images to be uploaded for every page. 我目前正在使用带有jQuery Booklet的 jquery小册子Moleskine Notebook来制作一个日志,其中包含要为每个页面上传的图像。 what im doing is im looping the div of every page in php as im calling data from DB, but when i tried to use file upload form as follows , it only works on first page on. 我在做什么是im循环在php中的每个页面的div作为im从数据库中调用数据,但是当我尝试使用文件上传形式如下时,它仅在首页上起作用。 the upload button appeared on every other page but it does nothing, not even call the upload window. 上载按钮出现在其他页面上,但是什么也没做,甚至没有调用上载窗口。

every page has its own log_id $row['log_id'] . 每个页面都有自己的log_id $row['log_id']

so any solution is much appreciated. 因此,非常感谢任何解决方案。

if (mysqli_multi_query($conn, $sql)) {
 do {
  if ($result = mysqli_store_result($conn)) {
    while ($row = mysqli_fetch_array($result)){

echo '<div>';
echo '    <form method="post" name="upload_form" id="upload_form" enctype="multipart/form-data" action="php/multiple_upload.php">';
echo '      <input type="hidden" name="form_submit" value="1"/>';
echo '      <input type="file" class="hidden" name="images[]" id="upload-images" multiple >';
echo '      <input name="log_id" type="hidden" value="'.$row['log_id'].'">' ;
echo '    </form>'; 
echo '</div>' ; 
            }
        mysqli_free_result($result);
        }   
    } while (mysqli_next_result($conn));
}

and the here is the script for calling form 这是调用表单的脚本

$(document).ready(function(){
$('#upload-images').on('change',function(){
    $('#upload_form').ajaxForm({
        beforeSubmit:function(e){
            $('.progress').show();
        },
        success:function(e){
            $('.progress').hide();

                location.reload(true);              
        },
        error:function(e){

            }
        }).submit();
    });
});

If you're using id $('#upload_form').ajaxForm({***}); 如果您使用的是ID $('#upload_form').ajaxForm({***}); . This will only find and target the first #upload_form 这只会找到并定位第一个#upload_form

And if you're using class like $('.upload_form').ajaxForm({***}); 如果您使用的是$('.upload_form').ajaxForm({***}); . This will find and target all the form class .upload_form 这将查找并定位all表单类.upload_form

You need to define which form you're looking for. 您需要定义要查找的form In this case, You just need to find the closest/parent of .upload-images form. 在这种情况下,您只需要查找.upload-images表单的最接近/父对象。 Like below. 像下面。

Do like this. 这样吧

$(document).ready(function(){
$('.upload-images').on('change',function(){
    $(this).closest('form').ajaxForm({ // target the parent form
        beforeSubmit:function(e){
            $('.progress').show();
        },
        success:function(e){
            $('.progress').hide();

                location.reload(true);              
        },
        error:function(e){

            }
        }).submit();
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM