connecting button to modal with bootstrap

Viewed 1071

I need some help connecting a button which pops up a modal to submit form.

this is my html before the modal (works fine and another page is rendered after button is clicked)

<div class="container my-container">
    <h3>
        Let's get started
    </h3>
        <div class="row my-row">
            <h4>1. CSV file</h4>
        </div>
        <div class="row my-row">
            <h4>2. There should be only two columns</h4>
        </div>
        <div class="row my-row">
            <form method="post" enctype="multipart/form-data">
                {%csrf_token%}
                <input type="file" name="document"><br>
                <button type="submit">  Submit</button>
            </form>
        </div>
</div>

I have added the modal but I now have a couple of issues

<div class="container my-container">
    <div class="row my-row">
    <h3>
        Let's get started
    </h3>
    </div>
    <div class="modal fade" id="demo123">
        <div class="modal-dialog">
            <div class="modal-content">
                <button type="button" class="close" data-dismiss="modal">
                    <span>
                        &times;
                    </span>
                </button>
                <div class="modal-header">
                    <h2 class="modal-title">
                        Confirm upload
                    </h2>
                </div>
                <div class="modal-body">
                    <p>this is the file name</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">
                        Confirm
                    </button>
                </div>
            </div>
        </div>
    </div>
        <div class="row my-row">
            <h4>1. CSV file</h4>
        </div>
        <div class="row my-row">
            <h4>2. There should be only two columns titled</h4>
        </div>
        <div class="row my-row">
            <form method="post" enctype="multipart/form-data">
                {%csrf_token%}
                <input type="file" name="document"><br>
                <button class="btn btn-primary" data-toggle="modal" data-target="#demo123">
                 Submit
                </button>
            </form>
        </div>
</div>

First issue I have is the "submit" button automatically makes the upload(renders to another page). How can I make so only if "confirm" button is the one which actually submits the form (if "x" then pop-up closes). Also how can I display the filename in the pop-up?

I use django with bootstrap 4

2 Answers

You have to use Javascript and since Bootstrap uses jQuery, you'll have to create that logic using Javascript and jQuery.

  1. Handle the dialog confirm button click and only then trigger a form submit event.
  2. Handle modal show event show.bs.modal and set the dialog filename element if there is a file selected and enable the confirm button; if there is no file selected, message the user about it using the same filename element and disable the confirm button so the user can't click it.

You can run the code snippet and test the whole functionality. On form submission we cancel the event and print a message in the console just for demonstration.

Please read inline comments

jQuery(function($) {

  // Handle form submit.
  // This won't be necessary unless you want some custom behavior.
  // Here we just cancel the form submission and just display a message for demonstration
  $('#uploadForm').on('submit', function(e) {
    console.log('The form is submitting; preventing event for demonstration');
    e.preventDefault();
  });

  // Handle dialog confirm button click.
  $('#confirmUpload').on('click', function(e) {
    // Submit the form and hide the dialog when confirm clicked
    $('#demoModal123').modal('hide');
    $('#uploadForm').submit();
  });
  
  // When the dialog is about to show, check if the user has selected a file.
  // If they have, update dialog filename element and enabled the confirm button,
  // else message user that there is no file and disable the confirm button
  $('#demoModal123').on('show.bs.modal', function() {   
    let files = document.getElementById('documentFile').files;
    let fileSelected = files && files[0];
    
    if (fileSelected) {
      $('#demoModal123 .filename').text(fileSelected.name);
      $('#confirmUpload').prop('disabled', false);
    } else {
      $('#demoModal123 .filename').text('No file selected');
      $('#confirmUpload').prop('disabled', true);
    }
  });
  
  // Update file input group control label on file selection
  $('#documentFile').on('change', function() {
    $('#documentFileName').text(this.files[0].name);
  });

});
<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

<div class="container my-container">
  <h3>Let's get started</h3>
  <div class="row my-row">
    <h4>1. CSV file</h4>
  </div>
  <div class="row my-row">
    <h4>2. There should be only two columns</h4>
  </div>
  <div class="row my-row">
    <form method="post" enctype="multipart/form-data" id="uploadForm">
      <!-- {%csrf_token%} -->     
      <div class="input-group">
        <div class="input-group-prepend">
          <span class="input-group-text" id="inputGroupFileAddon01">Upload</span>
        </div>
        <div class="custom-file">
          <input type="file" class="custom-file-input" name="documentFile" id="documentFile">
          <label class="custom-file-label" id="documentFileName" for="documentFile">Choose file</label>
        </div>
      </div>
      
      <button class="btn btn-primary" type="button" data-toggle="modal" data-target="#demoModal123">Submit</button>
    </form>
  </div>
</div>

<!-- Modal -->
<div class="modal fade" id="demoModal123">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="exampleModalLabel">Confirm upload</h2>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p class="filename">this is the file name</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
        <button type="button" class="btn btn-primary" id="confirmUpload">Confirm</button>
      </div>
    </div>
  </div>
</div>

To be able to act Confirm button as only submission, you'll have to add type="submit" in Confirm button and type="button" in Submit button. Like this:

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#demo123">
  Submit
</button>

<button type="submit" class="btn btn-secondary" data-dismiss="modal">
  Confirm
</button>
Related