Computer >> Computer tutorials >  >> Programming >> PHP

Download file through an AJAX call in PHP


Using Ajax to download files is not considered to be a good idea. Instead, window.location = or document.location should be used.

The 'window.location' has the following characteristics −

  • JavaScript enabling is required
  • It doesn't need PHP.
  • It helps show the content of the site, and redirects the user after a few seconds.

Redirect can be dependent on any conditions such as −

$success = 1
if ($success) {
   window.location.href = 'https://example.com';
}

A variable named ‘success’ is assigned with the value of 1. When this condition is satisfied, the window.location is used.

On running, the user is redirected to the website 'https://example.com'