简体   繁体   English

如何从JavaScript运行PHP脚本

[英]How to run a php script from javascript

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. PHP中包含代码,用于检查用户是否已登录到应用程序。 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. 如果用户未登录,我将使用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; 在我进行了一些测试之前,这非常有效,直到我手动退出FB并发现应用程序然后刷新应用程序时,Javascript帮助程序返回Null为止。 causing an error 100 (which is a access token error i assume). 导致错误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). 我假设我可以通过仅在javascript代码(当用户通过JS SDK连接到应用程序时)的certian条件期间调用PHP代码来避免此错误。 Facebook says you can access your PHP sdk from your javascript sdk with an AJAX call. Facebook说,您可以通过AJAX调用从javascript sdk访问PHP sdk。 They do not provide any further information and I am very new to this. 他们没有提供任何进一步的信息,对此我很陌生。

I have tried JQuery AJAX $.Get method and it seems to work but it doesn't run my PHP code?? 我已经尝试了JQuery AJAX $ .Get方法,它似乎可以工作,但是没有运行我的PHP代码? So i kept researching and found .Load. 因此,我一直在研究并发现.Load。 But it seems like i need to attach it to a DIV? 但似乎我需要将其附加到DIV上? is there another way to do this? 还有另一种方法吗? I just need to run the code, nothing more. 我只需要运行代码,仅此而已。

I am not sending data to the PHP code or returning data from PHP with the AJAX, the PHP just needs to be executed. 我不是在使用AJAX将数据发送到PHP代码或从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? 我应该在寻找替代解决方案吗?

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. 如果在页面加载后需要当前信息而不重新加载页面,则可以使用javascript进行AJAX调用。 The classic and probably easiest way to do this is with jquery. 做到这一点的经典方法(可能是最简单的方法)是使用jquery。 Basicly what will happen is you'll create a PHP page that will simply return back JSON which javascript can interpet very easily. 基本上,将要发生的是您将创建一个PHP页面,该页面将仅返回JSON ,而javascript可以很容易地插入JSON

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. 用PHP生成JavaScript。 This is almost certainly bad practice but it does work for something simple. 几乎可以肯定,这是不好的做法,但是对于某些简单的事情却确实起作用。

<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 server right before you send them the page. 因此,基本上,您是在发送页面之前在服务器上生成JavaScript代码的。

Do a ajax get request. 做一个ajax获取请求。 You dont have to send any data with it. 您不必发送任何数据。 the php code will get executed. 的PHP代码将被执行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM