2014-01-29 41 views
0

我有按鈕,其中包含<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的認證碼。如果認證成功,則給出idname。我想在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'}; 

?> 

請讓我知道如果你需要進一步的解釋。

底線:

  1. link click而是執行redirect.php劇本,我希望它通過按鈕點擊事件函數執行。
  2. 獲取idname從redirect.php到index.php redirect腳本執行

後,我已經在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返回nameid

因此,這足以對其進行管理:$.get('index.php', function(data) { ... }

+1

使用AJAX來這麼做 –

+1

目前還不清楚是什麼'ob_start'有做任何事情,或者你想在'index.php'上實現什麼功能。您的代碼已經將用戶發送到'redirect.php',不是嗎?這不是預期的效果嗎?請詳細說明。 – David

+0

@David:請看我添加底線。 –

回答

1

要綁定到HTML按鈕的單擊事件,可以使用JavaScript。既然你用jQuery標記了這個問題,我會假設它的用處。事件處理程序看起來是這樣的:

$('#loginButton2').click(function() { 
    window.location.href = 'redirect.php'; 
}); 

注意:這有效地模擬了錨定點擊。如果你不是想更接近於一個HTTP重定向,你可能想使用它代替:

window.location.replace('redirect.php'); 

至於idname值,究竟怎樣完成這個流程的用戶返回index.php第一地點?你的redirect.php有一個重定向(儘管不是所有的代碼路徑都會導致這種情況),所以它假定了非AJAX交互。 (我認爲XHR有時會遵循重定向,但行爲是不同的瀏覽器到另一個。)

如果重定向不是非常重要,你只是想打一個AJAX調用redirect.php,那麼你可以做到這一點一個簡單的AJAX請求:

$.get('redirect.php'); 

爲了獲取這些值回頁面,他們將需要從redirect.php發出。事情是這樣的:

echo json_encode((object) array('id' => $id, 'name' => $name)); 

然後在客戶端的代碼,你將不得不在AJAX回調可用這些值:

$.get('redirect.php', function(data) { 
    // data.id is the id 
    // data.name is the name 

    // use these values client-side however you need 
}); 
+0

這幫了我很多。我更新了我的問題,請你再次感激。 –

+0

@Programming_crazy:我不清楚你在更新的代碼中試圖做什麼。如果您使用的是AJAX請求,則不需要'window.location.href ='行。另外,爲什麼你發佈一個AJAX請求到'index.php'而不是'redirect.php'?我認爲這是這裏的目標。 – David

+0

是的,這是目標。根據我原來的問題,你的回答是正確的。但後來我發現,在按鈕上單擊'redirect.php'腳本執行。依次調用'index.php'和'name'&'id'來自'index.php' –

0
<script> 
    $("#loginButton2").on('click',function(){ 
    window.location.href="redirect.php"; 
    }); 
    </script> 

和redirect.php文件

$_SESSION['id']=$id ; 
    $_SESSION['name']=$name; 

<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> 

<input type = "button" id = "loginButton2" class = "btn btn-primary" value = "Login | Twitter " style = "left:650px; margin-top: -32px; position:relative"/> 
+0

爲什麼投票請 –

+0

是的,爲什麼投了票?請解釋誰做的理由。它可能幫助karthick和我 –

+1

他需要'redirect.php'服務器端計算的值在計算後可用於'index.php'。這是如何實現的?這只是將用戶發送到'redirect.php',然後保留服務器端的值。 'a'鏈接本身已經做到了,不需要JavaScript代碼。當他需要做的只是向服務器發出請求並在後臺獲取兩個小值作爲響應時,會話狀態的使用並不是真正必要的,而額外的頁面加載將降低整體性能和用戶體驗。 – David