Prashant
Prashant

Reputation: 143

How to execute php code after html page load

after full html page load want to execute some php api's & script and display response

<html>
<body>
content 
<?php
//some php script execute after full page load
?>
</body>
</html>

Upvotes: 11

Views: 41665

Answers (2)

Tom Udding
Tom Udding

Reputation: 2294

PHP is a server-side programming language. It gets executed when you request a page. So you cannot execute it after the page has been loaded. If you need to check if something has loaded on the client side, you'll have to use a client-side programming language like JavaScript.

That way you can make a request to a PHP file (which will be executed) and get process everything that is returned by the file.

You can do this using JavaScript or jQuery (a JavaScript library):

<script type="text/javascript" src="jquery.js"></script>          
<script type="text/javascript">
    // Check if the page has loaded completely                                         
    $(document).ready( function() { 
        $('#some_id').load('php-file.php'); 
    }); 
</script> 

Upvotes: 16

Naresh Kumar
Naresh Kumar

Reputation: 1736

To do so you could make use of the Ajax calls after the page is rendered ,

$(window).load(function(){ /*This allows to execute your code after the  page is rendered fully */ 
    $.get("<path to php file>", function(data){
     /* you can then process the ajax response as needed */
    });
})

You can this (Javascript that executes after page load) ,to be helpful to load your script after page load

Upvotes: 7

Related Questions