I am a newbie to php
<?php
getDBData(){
//log the call
$fetchedData = myDbCode.fetchData();
return
}
?>
<script type="text/javascript">
dbData = <?php echo json_encode(getDBData()); ?>
</script>
As observed in the log that getDBData get called only once during the page loading and later on even with dbData = <?php echo json_encode(getDBData()); ?>
this code the call to getDBData()
doesn't happen.
Any idea why the call to getDBData() happening only on page load and not thenafter
How to call getDBData()
from javascript
We will use ajax to execute a PHP code and get a response from it without reloading the page (like in a live search). Let's go to the code! Here, we call the example. php file and we save its response on a variable that we called response.
Of course it is possible to add a cron job and then delete it after the job has been executed once, but it is not a very good method. To run a PHP script at a given time only once, use the Linux's at command. at schedules a job to be executed only once.
Save and open the file in the browser. Go to File > Save As… and save the file as "helloworld2. php”, and open it in your browser by using the address: http://localhost/helloworld2.php.
AJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page. Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.
You don't actually understand, how it works.
Javascript is a client-side language, which means, that it executes in web browser. PHP is server-side which mean it executes on server.
While handling request, first PHP is executed, that the response is returned to user, and then Javacript executes.
To communicate between client and server you can use ajax requests, which are basically simple http requests but without reloading whole page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With