如何从 javascript 运行 php 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25395395/
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-10-28 04:27:57  来源:igfitidea点击:

How to run a php script from javascript

javascriptphpjqueryajaxfacebook

提问by user3753569

I am using FaceBook's Javascript and PHP SDK's together in a web-based application.

我在基于 Web 的应用程序中同时使用 FaceBook 的 Javascript 和 PHP SDK。

I need to run a PHP script when a certain condition is met in my Javascript code.

当我的 Javascript 代码满足某个条件时,我需要运行一个 PHP 脚本。

There is code in the PHP to check if the user is logged in on the application. if the user is not logged in, I use Facebook's javascript helper to grab their session data from the javascript sdk and add it to the database then sign them in on the application.

PHP 中有代码来检查用户是否已登录应用程序。如果用户未登录,我会使用 Facebook 的 javascript 助手从 javascript sdk 中获取他们的会话数据并将其添加到数据库中,然后在应用程序上登录。

this worked great until I did some testing and found out when i manually sign out of FB and the application then refresh the application, the Javascript helper is returning Null; causing an error 100 (which is a access token error i assume).

这很有效,直到我做了一些测试,发现当我手动退出 FB 并且应用程序刷新应用程序时,Javascript 助手返回 Null;导致错误 100(我认为这是访问令牌错误)。

which i am assuming i can avoid this error by only calling the PHP code during a certian condition on the javascript code (when user is connected to the application via JS SDK). Facebook says you can access your PHP sdk from your javascript sdk with an AJAX call. They do not provide any further information and I am very new to this.

我假设我可以通过仅在 javascript 代码的特定条件期间调用 PHP 代码来避免此错误(当用户通过 JS SDK 连接到应用程序时)。Facebook 说您可以通过 AJAX 调用从您的 javascript sdk 访问您的 PHP sdk。他们没有提供任何进一步的信息,我对此很陌生。

I have tried JQuery AJAX $.Get method and it seems to work but it doesn't run my PHP code?? So i kept researching and found .Load. But it seems like i need to attach it to a DIV? is there another way to do this? I just need to run the code, nothing more.

我试过 JQuery AJAX $.Get 方法,它似乎可以工作,但它不运行我的 PHP 代码??所以我一直在研究并找到了.Load。但似乎我需要将它附加到 DIV?有没有另一种方法可以做到这一点?我只需要运行代码,仅此而已。

I am not sending data to the PHP code or returning data from PHP with the AJAX, the PHP just needs to be executed.

我没有将数据发送到 PHP 代码或使用 AJAX 从 PHP 返回数据,只需要执行 PHP。

  function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);

// for FB.getLoginStatus()
if (response.status === 'connected') {

  // Fire the PHP code 

} else if (response.status === 'not_authorized') {

  console.log('Please log into facebook...')
} else {

}

}

}

Is it possible to Run my php file from javascript, then return to the javascript code?

是否可以从 javascript 运行我的 php 文件,然后返回到 javascript 代码?

Should I be looking at an alternative solution?

我应该寻找替代解决方案吗?

回答by JustGage

You can't run php code in the browser directly you have to do one of two things:

你不能直接在浏览器中运行 php 代码,你必须做两件事之一:

1. AJAX (probably the right way)

1. AJAX(可能是正确的方式)

if you need current information after the page loads without reloading the page you can make an AJAX call from javascript. The classic and probably easiest way to do this is with jquery. Basicly what will happen is you'll create a PHP page that will simply return back JSONwhich javascript can interpet very easily.

如果您在页面加载后需要当前信息而无需重新加载页面,您可以从 javascript 进行 AJAX 调用。执行此操作的经典且可能最简单的方法是使用 jquery。基本上会发生的是,您将创建一个 PHP 页面,该页面将简单地返回JSON,JavaScript 可以非常轻松地对其进行交互。

In PHP land:

在 PHP 领域:

<?php
$someVar = "text that needs to be in javascript"
echo $someVar;
?>

back in Javascript land:

回到 Javascript 领域:

var promise = $.getJSON("http://link_to_php_page.php");

promise.done(function(data) {
    console.log(data); 
    // this will return back "text that needs to be in javascript"
})

2. PHP injection (probably the wrong way)

2.PHP注入(可能是方法不对)

Generate JavaScript with PHP. This is almost certainly bad practice but it does work for something simple.

用 PHP 生成 JavaScript。这几乎肯定是不好的做法,但它确实适用于一些简单的事情。

<script>
   var textInJavascript = <?php echo 'text from php'; ?>;
</script>

NOTE:this is only run at page request time. So basically you're generating JavaScript code on the serverright before you send them the page.

注意:这仅在页面请求时运行。所以基本上你是在向服务器发送页面之前在服务器上生成 JavaScript 代码。

回答by user2909494

Do a ajax get request. You dont have to send any data with it. the php code will get executed.

做一个ajax获取请求。您不必随它发送任何数据。php 代码将被执行。

$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            url:"the_url",
            success:function(result){
                $("#adiv").html(result);
            }
        });
    });
});