user3753569
user3753569

Reputation: 1853

How to run a php script from 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?

Upvotes: 0

Views: 19001

Answers (2)

JustGage
JustGage

Reputation: 1652

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.

Upvotes: 3

user2909494
user2909494

Reputation: 13

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

Upvotes: 0

Related Questions