简体   繁体   中英

handling nested file include in php and returning value to javascript function

In login.php on button click I execute redirect.php . This redirect.php perform twitter authentication. For that it invokes some other file which in turn invokes index.php .

index.php gives result name and id , which I want to retrieve in index.php .

source code : Git source code

Currently what happens, login.php executes $.get() before response from login.php and alerts indefined value

login.php

    <body>
            <input type="button" value="Run somePHPfile.php" id = "b1" />
    <script>    
    $('#b1').click(function () {
        window.location.href = 'redirect.php';    

//This function needs improvement i think, is this correct place for this to get json value from index.php??
    $.get('index.php', function(data) { //If I put this out side click then it gives undefined value for name and id before redirect.php gets executed
        // data.id is the id
            var id= data.id;
            var name = data.name;
            alert(name);
            });
    });
    </script>
    </body>

redirect.php

<?php

/* Start session and load library. */
session_start();
require_once('twitteroauth/twitteroauth.php');
require_once('config.php');

... //some other processing
?>

index.php

<?php
    session_start();
    require_once('twitteroauth/twitteroauth.php');
    require_once('config.php');

    if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
        header('Location: ./clearsessions.php');
    }
    $access_token = $_SESSION['access_token'];
    $connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);
    $content = $connection->get('account/verify_credentials');
    $twitteruser = $content->{'screen_name'};
    $notweets = 5;
    $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

        $name = $content->{'name'}; 
    $id = $content->{'id'}; 
echo json_encode((object) array('id' => $id, 'name' => $name)); //to retreive in login.php
?>

UPDATE

$.get('index.php', function(data) { var user_id= data.id; var name = data.name; alert(name); // alert(name); window.location.href = 'button.php';

},"json");

since your php file is outputting a json string your get call needs to know that it is retriving such

$.get('index.php', function(data) {
   var id= data.id;
   var name = data.name;
   alert(name);
},"json");

This will let jquery know that it needs to parse the incoming data as a json string and parse it to an object, otherwise you would have to do the parse yourself

$.get('index.php', function(data) {
   data = JSON.parse(data);
   var id= data.id;
   var name = data.name;
   alert(name);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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