如何在不重新加载页面的情况下运行php函数

时间:2023-01-02 01:09:23

I am a newbie to php

我是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.

正如日志中所观察到的,getDBData在页面加载期间只被调用一次,后来甚至用dbData = <?php echo json_encode(getDBData()); ?>此代码不会调用getDBData()。

Any idea why the call to getDBData() happening only on page load and not thenafter

任何想法为什么调用getDBData()只发生在页面加载而不是之后

How to call getDBData() from javascript

如何从javascript调用getDBData()

7 个解决方案

#1


14  

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.

Javascript是一种客户端语言,这意味着它在Web浏览器中执行。 PHP是服务器端,这意味着它在服务器上执行。

While handling request, first PHP is executed, that the response is returned to user, and then Javacript executes.

在处理请求时,首先执行PHP,将响应返回给用户,然后执行Javacript。

To communicate between client and server you can use ajax requests, which are basically simple http requests but without reloading whole page.

要在客户端和服务器之间进行通信,您可以使用ajax请求,这些请求基本上是简单的http请求,但不会重新加载整个页面。

#2


8  

You should use Ajax for that. I.e. you have a php file which returns the output of the function:

你应该使用Ajax。即你有一个PHP文件,它返回函数的输出:

// 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 code above uses jQuery.

上面的代码使用jQuery。

#3


0  

You can do it through ajax.

你可以通过ajax来做到这一点。

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

这里有一个用jquery做的链接:使用jquery $ .ajax来调用PHP函数

#4


0  

use jquery

使用jquery

$.ajax({
            url: 'yourpage.php',
            type: 'POST',
            data:'',
            success: function(resp) {

            // put your response where you want to  

            }
        }); 

#5


0  

You can't directly call PHP functions from javascript.

你不能直接从javascript调用PHP函数。

You have to "outsource" the getDBDate to an own .php file where you output the json_encoded string and call this file with ajax and get the output of the page.

您必须将getDBDate“外包”到自己的.php文件中,在该文件中输出json_encoded字符串并使用ajax调用此文件并获取页面的输出。

The easiest to do AJAX requests in javascript is to use the JQuery Library: http://api.jquery.com/jQuery.ajax/

在javascript中最容易做的AJAX请求是使用JQuery库:http://api.jquery.com/jQuery.ajax/

#6


0  

you can used AJAX for get server side php vaue into javascript variable read this ajax example and implement it.

你可以使用AJAX来获取服务器端的php vaue到javascript变量中读取这个ajax示例并实现它。

            // 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 );                    
        }
        );

#7


-7  

you actually use AJAX if you want to re-call a data after a php page has loaded. but the example you have posted above, from my understanding it sounds like you just need to get PHP response inside a SCRIPT tag.

如果你想在加载php页面后重新调用数据,你实际上使用AJAX。但是你上面发布的例子,从我的理解来看,你只需要在SCRIPT标签内获得PHP响应。

also you have need to return value from within a function, like this.

你也需要从函数中返回值,就像这样。

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

#1


14  

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.

Javascript是一种客户端语言,这意味着它在Web浏览器中执行。 PHP是服务器端,这意味着它在服务器上执行。

While handling request, first PHP is executed, that the response is returned to user, and then Javacript executes.

在处理请求时,首先执行PHP,将响应返回给用户,然后执行Javacript。

To communicate between client and server you can use ajax requests, which are basically simple http requests but without reloading whole page.

要在客户端和服务器之间进行通信,您可以使用ajax请求,这些请求基本上是简单的http请求,但不会重新加载整个页面。

#2


8  

You should use Ajax for that. I.e. you have a php file which returns the output of the function:

你应该使用Ajax。即你有一个PHP文件,它返回函数的输出:

// 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 code above uses jQuery.

上面的代码使用jQuery。

#3


0  

You can do it through ajax.

你可以通过ajax来做到这一点。

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

这里有一个用jquery做的链接:使用jquery $ .ajax来调用PHP函数

#4


0  

use jquery

使用jquery

$.ajax({
            url: 'yourpage.php',
            type: 'POST',
            data:'',
            success: function(resp) {

            // put your response where you want to  

            }
        }); 

#5


0  

You can't directly call PHP functions from javascript.

你不能直接从javascript调用PHP函数。

You have to "outsource" the getDBDate to an own .php file where you output the json_encoded string and call this file with ajax and get the output of the page.

您必须将getDBDate“外包”到自己的.php文件中,在该文件中输出json_encoded字符串并使用ajax调用此文件并获取页面的输出。

The easiest to do AJAX requests in javascript is to use the JQuery Library: http://api.jquery.com/jQuery.ajax/

在javascript中最容易做的AJAX请求是使用JQuery库:http://api.jquery.com/jQuery.ajax/

#6


0  

you can used AJAX for get server side php vaue into javascript variable read this ajax example and implement it.

你可以使用AJAX来获取服务器端的php vaue到javascript变量中读取这个ajax示例并实现它。

            // 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 );                    
        }
        );

#7


-7  

you actually use AJAX if you want to re-call a data after a php page has loaded. but the example you have posted above, from my understanding it sounds like you just need to get PHP response inside a SCRIPT tag.

如果你想在加载php页面后重新调用数据,你实际上使用AJAX。但是你上面发布的例子,从我的理解来看,你只需要在SCRIPT标签内获得PHP响应。

also you have need to return value from within a function, like this.

你也需要从函数中返回值,就像这样。

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