How to run php function without page reload

I'm new to php

<?php getDBData(){ //log the call $fetchedData = myDbCode.fetchData(); return } ?> <script type="text/javascript"> dbData = <?php echo json_encode(getDBData()); ?> </script> 

As noted in the log, getDBData is called only once during page load, and later even with dbData = <?php echo json_encode(getDBData()); ?> dbData = <?php echo json_encode(getDBData()); ?> this code does not call getDBData() .

Any idea why the getDBData () call only happens when the page loads, not after

How to call getDBData() from javascript

+4
source share
7 answers

You really don't understand how this works.

Javascript is a client language, which means that it runs in a web browser. PHP is server-side, which means that it runs on the server.

When processing the request, the first PHP is executed, the response is returned to the user, and then Javacript is executed.

For communication between the client and the server, you can use ajax requests, which are basically simple HTTP requests, but without reloading the entire page.

+14
source

You must use Ajax for this. That is, you have a php file that returns the result of the function:

 // data.php <?php function getDBData(){ //log the call $fetchedData = myDbCode.fetchData(); return $fetchedData; } echo getDBData(); ?> // html file <script type="text/javascript"> var getDBData = function(callback) { $.ajax({ url: "data.php" }).done(callback); } var dbData = <?php echo json_encode(getDBData()); ?> getDBData(function(data) { dbData = data; }) </script> 

The above code uses jQuery .

+8
source

You can do this via ajax .

Here is a link here to do it using jquery: using jquery $ .ajax to call a PHP function

0
source

use jquery

 $.ajax({ url: 'yourpage.php', type: 'POST', data:'', success: function(resp) { // put your response where you want to } }); 
0
source

You cannot directly call PHP functions from javascript.

You must "outsource" getDBDate to your own .php file, where you output the json_encoded line and call this file with ajax and get the page output.

The easiest way to make AJAX requests in javascript is to use the jQuery library: http://api.jquery.com/jQuery.ajax/

0
source

you can use AJAX to get php vaue server side in javascript variable to read this ajax example and implement it.

  // Launch AJAX request. $.ajax( { // The link we are accessing. url: jLink.attr( "href" ), // The type of request. type: "get", // The type of data that is getting returned. dataType: "html", error: function(){ ShowStatus( "AJAX - error()" ); // Load the content in to the page. jContent.html( "<p>Page Not Found!!</p>" ); }, beforeSend: function(){ ShowStatus( "AJAX - beforeSend()" ); }, complete: function(){ ShowStatus( "AJAX - complete()" ); }, success: function( strData ){ ShowStatus( "AJAX - success()" ); // Load the content in to the page. jContent.html( strData ); } } ); // Prevent default click. return( false ); } ); 
0
source

you are actually using AJAX if you want to recall data after loading the php page. but the example you posted above, from my understanding, it sounds like you just need to get the PHP response inside the SCRIPT tag.

also you need to return a value from a function, e.g.

  <?php getDBData(){ return myDbCode.fetchData(); } ?> 
-7
source

Source: https://habr.com/ru/post/1500288/


All Articles