Should I rename my .html in .php when using AJAX?

Viewed 68

I have an index.html page, in which I have a button that runs php code through Javascript, using XMLHttpRequest.

The php code just reads index.html and make computations, but it does not inject php code.

Since the code is not working, I am wondering if I have to rename my file index.html to index.php. Should I do it?

2 Answers

Adding to Jesse's answer:

You only have to rename the page to .php if there is PHP code on that page that needs to be executed. If that is not the case, you can leave it as .html.

Also, make sure you don't have caching enabled for .html files, as generally, being a static file, AJAX tends to get the cached version of it. You can disable AJAX caching by two ways:

Using XHR's caching:

xhr.setRequestHeader('Cache-Control', 'no-cache');

Or, appending a query string:

xhr.open('GET', 'file.html?cachebuster=' + new Date().getTime());

You only have to rename the page to .php if there is PHP code on that page that needs to be executed. If that is not the case, you can leave it as .html.

Related