douxie3625 2018-09-04 13:41
浏览 131
已采纳

如何在多文件上传公式中动态添加“input type = file”按钮[PHP,MySQL&JS]

I have a problem by adding ""input type=file"" dynamically.

I have a formula where the user can select multiple files and can type a title under each selected file, before the user click on the Upload button. When the forumla gets loaded, i want that there is only 1 select button and 1 title field. If the user selected 1 file, then there should show up a new "select file" button and a new title field. (Add buttons dynamically)

I think my problem has something to do with file.setAttribute("name", "sel_file"); in the JS part and with this for loop
for($mf = 0; $mf < count($_FILES['sel_file']['tmp_name']); $mf++)
in the php part. It seems php cannot work with "sel_file"


Anyway, here is my code (The whole code is in 1 php file):
(and ignore the security issues, I will do this later)


PHP Part

<?php
$db = mysqli_connect("localhost", "root", "", "xy");

// If upload button is clicked ...
if (isset($_POST['upload']))
{ // Get post title
  $post_title = mysqli_real_escape_string($db, $_POST['post_title']);

  $sql_p = "INSERT INTO posts (post_title) VALUES ('$post_title')";
  if (mysqli_query($db, $sql_p))
  {
    $post_id = mysqli_insert_id($db);
    for($mf = 0; $mf < count($_FILES['sel_file']['tmp_name']); $mf++)
    {
      $file = $_FILES['sel_file']['name'][$mf];
      $tmp_file = $_FILES['sel_file']['tmp_name'][$mf];
      $target = "files/". basename($file);
      $file_title = (isset($_POST['file_title'][$mf])?$_POST['file_title'][$mf]:"");
      if (move_uploaded_file($tmp_file, $target))
      {
        echo "File: ".$file." &#10004;<br>";
      }
      else
      {
        echo "File: ".$file." - Upload failed<br>";
      }
      $sql_f = "INSERT INTO files (file, file_title, post_id) VALUES ('$file', '$file_title', $post_id)";
      mysqli_query($db, $sql_f);
    }
  }
}
?>

HTML & JS Part

<!DOCTYPE html>
<html>

  <head>
    <title>Project | XY</title>
    <meta charset="UTF-8"/>
    <script>
      window.addEventListener("load", function()
      {
        document.getElementById("add").addEventListener("click", function()
        {
          // Create a div
          var div = document.createElement("div");

          // Create a file input
          var file = document.createElement("input");
          file.setAttribute("type", "file");
          file.setAttribute("name", "sel_file"); 
          // Create a text input
          var text = document.createElement("input");
          text.setAttribute("type", "text");
          text.setAttribute("name", "file_title");

          // add the file and text to the div
          div.appendChild(file);
          div.appendChild(text);

          //Append the div to the container div
          document.getElementById("container").appendChild(div);
        });
      });
    </script>
  </head>

  <body>
    <div id="frame_form">
      <form method="POST" action="TEST.php" enctype="multipart/form-data">
        <div id="post_title">
          <input type="text" name="post_title" placeholder="* Post Title *">
        </div>
        <br>
        <form>
           <div>
              <input type="button" value="add" id="add" />
                <div id="container">&nbsp;</div>
           </div>
         </form>
        <br>
        <div id="upload_button">
          <button type="submit" name="upload">+upload</button>
        </div>
      </form>
    </div>
  </body>

</html>


Before i had no JS code, instead i had the following code, and it worked very well, but was not dynamically ...:

    <?php $sfq = 2 ?>
    <?php
        for ($sf=0; $sf<$sfq; $sf++){ ?>
        <b><?php echo $sf+1 ?></b><br>
        File: <input type="file" name="sel_file[<?php echo $sf ?>]"><br>
        Title: <input name="file_title[<?php echo $sf ?>]"><br>
    <?php } ?>
  • 写回答

1条回答 默认 最新

  • dongmi4720 2018-09-04 13:51
    关注

    Since your PHP is expecting those two fields to be arrays, you need to be sure you are setting the name of those input fields to behave as an array when posted.

    So where you have the JS of:

          var file = document.createElement("input");
          file.setAttribute("type", "file");
          file.setAttribute("name", "sel_file"); 
    
          var text = document.createElement("input");
          text.setAttribute("type", "text");
          text.setAttribute("name", "file_title");
    

    Change the name assignments to:

          var file = document.createElement("input");
          file.setAttribute("type", "file");
          file.setAttribute("name", "sel_file[]"); 
    
          var text = document.createElement("input");
          text.setAttribute("type", "text");
          text.setAttribute("name", "file_title[]");
    

    Note the brackets. This makes php turn those multiple named inputs into an array, so the rest of your PHP will work correctly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置