0

I am using the following script for a file upload progress bar. Everything works fine except for the echo command in my php script test2.php.

Once the file is uploaded, I do an echo to display "File Uploaded" with the bold tags within the test2.php and the print out will display "File Uploaded" along with the bold tags as opposed to making it bold.

Nothing is wrong with the test2.php becuase if I go to that page directly, then that text is printed in bold.

$(function() {
  $('#btn').click(function() {
    $('.myprogress').css('width', '0');
    $('.msg').text('');
    var filename = $('#filename').val();
    var myfile = $('#myfile').val();
    var formData = new FormData();
    formData.append('myfile', $('#myfile')[0].files[0]);
    formData.append('filename', filename);
    $('#btn').attr('disabled', 'disabled');
    $('.msg').text('Uploading in progress...');
    $.ajax({
      url: 'test2.php',
      data: formData,
      processData: false,
      contentType: false,
      type: 'POST',
      // this part is progress bar
      xhr: function() {
        var xhr = new window.XMLHttpRequest();
        xhr.upload.addEventListener("progress", function(evt) {
          if (evt.lengthComputable) {
            var percentComplete = evt.loaded / evt.total;
            percentComplete = parseInt(percentComplete * 100);
            $('.myprogress').text(percentComplete + '%');
            $('.myprogress').css('width', percentComplete + '%');
            $('.myProgress').css("background-color", "#0078ae");
          }
        }, false);
        return xhr;
      },
      success: function(data) {
        $('.msg').text(data);
        $('#btn').removeAttr('disabled');
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform1" method="post" enctype="multipart/form-data">
  <div class="form-group">
    <label>Rename Firmware File <i>(optional)</i> </label>
    <input class="form-control" type="text" id="filename" />
  </div>
  <div class="form-group">
    <input type="file" id="myfile" />
  </div>
  <div class="form-group">
    <div class="progress">
      <div class="progress-bar progress-bar-success myprogress" role="progressbar" style="width:0%;">0%</div>
    </div>
    <div class="msg"></div>
  </div>
  <input type="button" id="btn" class="ui-button" value="Upload" />
</form>

3
  • 1
    Have you watched the AJAX request in the browser's developer tools? Commented Jan 12, 2018 at 16:33
  • 2
    If you want to include HTML in the message, use the html method instead of text ($('.msg').html(data);) Commented Jan 12, 2018 at 16:38
  • Thanks Chris, that was it. Commented Jan 12, 2018 at 16:45

2 Answers 2

1

In the success handler, you're calling .text to set the text on an element, but that essentially escapes HTML.

Instead, try .html (just be careful that you're sending sanitized data!)

success: function(data) {
    $('.msg').html(data);
    $('#btn').removeAttr('disabled');
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try adding the following the settings object you pass into $.ajax():

dataType: "html"

Accoding to the jQuery API docs, the dataType setting specifies "...The type of data that you're expecting back from the server", and setting it to html "Returns HTML as plain text; included script tags are evaluated when inserted in the DOM."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.