Running an external PHP file from Javascript

I have a html page. Each time this is loaded I’m wanting an external PHP script to run.

For a number of reasons, I am not wanting to have the HTML page as a PHP page and just have the script written in there.

I am led to believe that the best way to do this is to use Javascript, if I’m wrong then I am open to other suggestions.

From what I’ve found from reading on the web, I have come up with this to put in my HTML page (it’s towards the end just before the closing body tag):

<script type="text/javascript" language="javascript">$(document).ready(function(){$.ajax({url: 'cnter/cnter.php'}); });</script>

The cnter.php script is located in a sub-folder below the HTML file. It does not require any arguments to be passed to it, and it does not need to send anything back, it just needs to execute. The PHP file, when loaded directly in a browser, runs and does what it’s supposed to, I have tested that.

Just need this file to run when the HTML page is loaded, or is it not possible? At the moment, it’s not doing this.

Any help and/or suggestions gratefully received.


You can use jQuery’s load() function to get content from another URL and insert into the current page:


Great stuff. More simple than I was making it.

Thank you.