最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to run a php script from javascript - Stack Overflow

programmeradmin5浏览0评论

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?

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?

Share Improve this question edited Aug 20, 2014 at 1:51 user3753569 asked Aug 20, 2014 at 1:24 user3753569user3753569 1,8535 gold badges15 silver badges18 bronze badges 2
  • 1 Well, can you put down your code (or similar code) here and explain your question with fewer words? That would be great. :) – shrekuu Commented Aug 20, 2014 at 1:34
  • I just dont fully understand AJAX is the issue, and if its even the solution i am looking for. How do I execute a PHP file from javascript? Facebook said use an AJAX call to call the PHP from the javascript. This is what I am trying to achieve: JS checks their status >> status is connected >> fire PHP >> return to application – user3753569 Commented Aug 20, 2014 at 1:48
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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

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

评论列表(0)

  1. 暂无评论