How to Execute PHP Code after HTML Page Load


Categories
Tags
Got some code you’d like to run after your HTML page has loaded? Here’s how to execute some PHP after the rest of the page has loaded, using jQuery.

PHP is server side, so it’s pretty much impossible to do this without using a client-side language such as JavaScript or Ajax. In this example, we’ll be using jQuery’s load() method, so you’ll need to include the jQuery library. Example (within your head tag is usually best):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

After that, create a placeholder on your page for where you want your external content to be loaded into:

<div id="loading-area">
	<p style="text-align:center">Loading...</p>
</div>

Lastly, using jQuery’s load function, we’ll add this page into the placeholder created above. This will replace anything within our loading-area div, replacing it with our external page’s content/code. Put this bit at the bottom of your page so it runs after the rest of your page has loaded. Be sure to get the path to your file right:

<script>$('#loading-area').load('/testpage.php');</script>

Alternatively, wrap a window load event around it like this (jQuery below v3):

<script>
	$(window).load(function(){
		$('#loading-area').load('/testpage.php');
	})
</script>

Or like this (jQuery V3+)…

<script>
	$(window).on('load', function () {
		$('#loading-area').load('/testpage.php');
	})
</script>

Comments (2)

Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.

  • Problem right off the bat: HOW do you load the JQuery library?

    Comment by not necessary (12/06/2023)
  • Example now added to the article above. Hope it helps!

    Comment by Andy (13/06/2023)

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the author(s) and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views. Nothing negative please.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).

Thank you – we hope you find the website useful.