Skip to content
Advertisement

Why does my XMLHttpRequest aborts on PHP file upload?

I’m trying to upload a video file via AJAX, a XMLHttpRequest and PHP to display the upload progress but the request throws an abort and quickly refreshes the page if I try to upload something.

If I just send the POST request to the PHP file via the form, without AJAX/XMLHttpRequest, the file upload works. But a progress bar would be handy…

The code consists of the HTML form, the JavaScript functions and the PHP upload (I omitted the progress bar HTML code to make it more readable).
HTML:

JavaScript

JavaScript:

JavaScript

And PHP:

JavaScript

Any ideas why the request immediately aborts and refreshes the page? The PHP log is empty.

The environment is local via XAMPP

Advertisement

Answer

You’re submitting the form. The JS starts but then the form submits and you navigate to a new page (which aborts all the JS from the previous page).

  1. Remove the onclick attribuete
  2. Bind a submit event handler on the form
  3. Prevent the default behaviour that comes with form submissions

Such:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement