我有按鈕,其中包含<a>
標記。點擊後,它會執行redirect.php
腳本。執行php腳本按鈕單擊並返回值ob_start
的login.php - 包含
<a href="./redirect.php"><input type = "button" id = "loginButton2" class = "btn btn-primary" value = "Login | Twitter " style = "left:650px; margin-top: -32px; position:relative"/> </a>
redirect.php
包含Twitter的認證碼。如果認證成功,則給出id
和name
。我想在index.php中獲取這兩個值
使用ob_start();
我可以通過json從php腳本接收JS函數的值。
但我很困惑如何管理index.php
中的代碼來執行腳本按鈕單擊並接收這兩個值。
redirect.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);
$id = $content->{'id'};
$name = $content->{'name'};
?>
請讓我知道如果你需要進一步的解釋。
底線:
- 上
link click
而是執行redirect.php
劇本,我希望它通過按鈕點擊事件函數執行。 - 獲取
id
和name
從redirect.php到index.phpredirect
腳本執行
後,我已經在session_start()來管理Twitter的會議。因此,不想把事情弄糟使用,如果沒有必要多發會議..
UPDATE大衛的回答後
<body>
<input type="button" value="Run somePHPfile.php" id = "b1" />
<script>
$('#b1').click(function() {
window.location.href = 'redirect.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
腳本。 redirect.php
包含其他文件,最終到達index.php。 index.php
返回name
和id
。
因此,這足以對其進行管理:$.get('index.php', function(data) { ... }
使用AJAX來這麼做 –
目前還不清楚是什麼'ob_start'有做任何事情,或者你想在'index.php'上實現什麼功能。您的代碼已經將用戶發送到'redirect.php',不是嗎?這不是預期的效果嗎?請詳細說明。 – David
@David:請看我添加底線。 –