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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18591809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:52:20  来源:igfitidea点击:

how to run php function without reloading the page

phpjavascript

提问by veer7

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

如何getDBData()从javascript调用

回答by Jakub Matczak

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 请求,但无需重新加载整个页面。

回答by Krasimir

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

回答by kmas

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执行此操作的链接:using jquery $.ajax to call a PHP function

回答by Shadow

use jquery

使用jQuery

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

            // put your response where you want to  

            }
        }); 

回答by virtualmarc

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/

回答by Shakti Patel

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