Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run php function without reloading the page

Tags:

javascript

php

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

like image 689
veer7 Avatar asked Sep 03 '13 11:09

veer7


People also ask

How do I run a PHP file without reloading?

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.

How do I run a PHP script only once?

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.

How do I save and run PHP?

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.

How do I change the content of a page without reloading it?

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.


1 Answers

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.

like image 107
Jakub Matczak Avatar answered Sep 28 '22 13:09

Jakub Matczak