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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里