如何从JavaScript运行php脚本 [英] How to run a php script from javascript

查看:121
本文介绍了如何从JavaScript运行php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



当我的Javascript中遇到某种条件时,我需要运行一个PHP脚本代码。



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



这个工作很好,直到我做了一些测试,发现当我手动退出FB,应用程序然后刷新应用程序,Javascript帮助器返回Null;导致错误100(这是我假设的访问令牌错误)。



我假设我可以通过在certian条件下调用PHP代码来避免此错误JavaScript代码(当用户通过JS SDK连接到应用程序时)。 Facebook说您可以通过AJAX调用从您的javascript sdk访问您的PHP sdk。我们已经尝试了JQuery AJAX $ .Get方法,它似乎工作,但没有运行。我的PHP代码?
所以我一直在研究和发现。但是,似乎我需要把它附加到DIV?还有另一种办法吗?我只需要运行代码,没有更多的东西。



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

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

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

// Fire PHP代码

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

console.log('请登录Facebook ...')
} else {

}

}



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



我应该查看另一种解决方案吗?

解决方案

您不能直接在浏览器中运行php代码,您必须执行以下两项操作之一:



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



如果在页面加载后需要当前信息,而不重新加载页面,则可以从javascript进行AJAX调用。经典和可能最简单的方法是使用jquery。基本上会发生什么,你会创建一个PHP页面,只需返回 JSON 哪些javascript可以非常容易地插入。



在PHP土地:

 <?php 
$ someVar =需要在javascript中的文本
echo $ someVar;
?>

返回Javascript土地:

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

promise.done(function(data){
console.log(data);
//这将返回需要在javascript中的文本
})



2。 PHP注入(可能是错误的方式)



使用PHP生成JavaScript。这几乎肯定是不好的做法,但它对于简单的事情起作用。

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

注意:只能在页面请求时间即可。因此,您在发送他们的页面之前基本上是在服务器上生成JavaScript代码。


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

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

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.

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).

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.

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.

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

  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?

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:

1. AJAX (probably the right way)

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 JSON which javascript can interpet very easily.

In PHP land:

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

back in Javascript land:

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)

Generate JavaScript with PHP. 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运行php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆