Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have php code which I want to execute on button click event in function. I tried in various way with no luck. It's easy but I am new to this so could not do.

<?php
session_start();
$appId = '----'; 
$appSecret = '----'; // Facebook App Secret
$return_url = 'dev01.dev/FB/facebook-php-sdk/examples/';  //path to script folder
  ?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
 <script>

 function AjaxResponse()
 {

    alert("hi");
   }

function LodingAnimate() //Show loading Image
{
    $("#LoginButton").hide(); 
      AjaxResponse();    
}
 </script></head><body>
<?php
if(!isset($_SESSION['logged_in']))
{
?>
    <div id="results">    </div>
    <div class="results1">    </div>
    <div id="LoginButton">
    <button type="button" onclick="CallAfterLogin()" ?>Click Me!</button> 
    </div>
<?php
}
?>
<div id="fb-root"></div>
<script type="text/javascript">
//-------
window.fbAsyncInit = function() {
FB.init({
    appId: '<?php echo $appId; ?>',
    cookie: true,
    xfbml: true,
    channelUrl: '<?php echo $return_url; ?>channel.php',
    oauth: true});};

(function() {
    var e = document.createElement('script');
    e.async = true;
    e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);}());

function CallAfterLogin(){
        FB.login(function(response) {  //---
        if (response.status === "connected")
        {
            LodingAnimate();                    
         } //if
             }); //---
          } 
       <div id="results"> 
          </div>
       </script>
</body>
</html>

If possible then I want to execute in ajaxResponse function. My code is:

if (!class_exists('FacebookApiException')) {
    require_once('facebook.php' );
    }
        $facebook = new Facebook(array(
        'appId' => $appId,
        'secret' => $appSecret,
    ));
    $fbuser = $facebook->getUser();
    if ($fbuser) 
    {
            try {
             $user_interest = $facebook->api('/me/movies');echo '<pre>';
            foreach ($user_interest['data'] as $var)
        {
             $ID=$var['id'];
             $Name=$var['name'];
             echo '<br/>';
             print_r($Name); echo '<br/>';
             print_r($ID);
        }
        echo '</pre>';
    }
    catch (FacebookApiException $e) {
        echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
        $user = null;
    }
    }
share|improve this question
 
It's easy but I am new to this so could not do. contradicts to the fact you post a question but fail to supply why it is failing, but seems to be easy to solve. –  dbf Jul 31 at 11:45
1  
Have you done a 'view source' to see what the generated code looks like to the browser? That should give you good clues about what the problem might be. But in general, you best practice is to have as little dynamic JS code as possible. The better way of doing it is to generate a JSON array using php's json_encode(). Then the rest of your JS code should reference the values from there, rather than having dynamic PHP embedded in them. –  Spudley Jul 31 at 11:47
2  
seriously? this is the third stackoverflow thread you open for the same code. i already told you to get rid of the php stuff, you can do all that with the javascript sdk of facebook. and you reeeeally need to learn programming BEFORE starting with the facebook api, no offense... –  luschn Jul 31 at 12:05
 
You are right, it is easy. You click a button which sends a request to the server to run the php code and give you the results back. So which part of that are you having trouble with? –  Anigel Jul 31 at 12:40
 
@luschn: I agree :P but each thread I start give me something different to learn, I am learning programming. I will take care –  Karimkhan Jul 31 at 16:47
show 4 more comments

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.