简体   繁体   English

使用 Javascript 更新引导容器

[英]Updating Bootstrap container with Javascript

I have a Bootstrap container:我有一个引导容器:

 <div class="container">
      <div class="jumbotron">
        <div class="row">
          <div class="col-lg-8">
            <form action="upload.php" method="POST" enctype="multipart/form-data">
                <h3><strong>Step 1: Choose an image from your PC</strong></h3><hr>
                  <input type="file" name="file" id="inputFile"><br><br> 
                  <button type="submit" name="btn_submit" value="upload" class="btn btn-secondary">Select image</button>
            </form>
          </div>
       </div>
     </div>
 </div>

Once the upload.php processes the upload successfull I would like to update the existing row container and add a new column container displaying the image on the right hand side.一旦 upload.php 处理上传成功,我想更新现有的容器并添加一个新的列容器,在右侧显示图像。 The following HTML works I can change the php inside the HTML if theres an easier method:以下 HTML 有效,如果有更简单的方法,我可以更改 HTML 内的 php:

<div class="col-lg-4">
    <?php echo display_image($_SESSION['upload-file-destination']); ?><br>
    <p><strong>File size: <?php echo $_SESSION['file-size'] / 1024;?> KB</strong></p>
</div>

How can I achieve this using Javascript or Jquery?如何使用 Javascript 或 Jquery 实现此目的?

First create a xmlhttprequest or fetch call to your uplad.php file.首先创建一个xmlhttprequest获取对 uplad.php 文件的调用。 From your upload.php file return the file destination for example ./uploads/images/myFile.png .从您的 upload.php 文件返回文件目标,例如./uploads/images/myFile.png Now you can add a div, where your image should be placed and add it with javascript.现在您可以添加一个 div,您的图像应该放置在其中并使用 javascript 添加它。

html: html:

<div id="img"></div>

javascript: javascript:

let imgDiv = document.getElementByid("img");
let img = document.createElement("img");
img.setAttribute("src", "your_response_path"); // as mentioned above
imgDiv.appendChild(img);

Here is the solution to my problem:这是我的问题的解决方案:

<script type="text/javascript">
    $(document).ready(function() {
        $(".row").append('"<div class=\"col-lg-3\" id=\"img-upload\"><?php echo display_image($_SESSION['upload-file-destination']); ?></div>"');
        $("#img-upload").append('<br><p><strong>File size: <?php echo $_SESSION['file-size'] / 1024;?> KB</strong></p>');
    });
</script>

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

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