Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute php code after html page load

Tags:

javascript

php

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>
like image 794
Prashant Avatar asked Oct 22 '17 07:10

Prashant


People also ask

Can I run PHP code in HTML file?

You can't, unless you instruct Apache to treat . html files as PHP.


2 Answers

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> 
like image 122
Tom Udding Avatar answered Oct 11 '22 02:10

Tom Udding


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

like image 20
Naresh Kumar Avatar answered Oct 11 '22 03:10

Naresh Kumar