2015-02-07 124 views
0

我最近發現一個很酷的標籤內容頁面,當你點擊標籤,他們表現出的內容http://codepen.io/unasAquila/pen/nDjgI我發現的是,一旦你進入的頁面,這些標籤被預裝,而不是裝的標籤點擊。我想知道是否可以在點擊選項卡時將內容加載到內容的位置。舉例來說,如果我有,我想加載信息,比如這個PHP查詢語句:加載內容點擊

$query3 = $db->query("SELECT * FROM mybb_game WHERE id='" . $id . "'"); 

while($row = mysqli_fetch_array($query3)) 
{ 
    echo "$row[name] 
} 

我怎麼會讓它的地方,一旦標籤被點擊的內容只裝?

+0

關於代碼問題的問題應該在問題本身**中提供相關代碼**。瞭解如何製作[MCVE](/ help/mcve),然後編輯您的問題以改進它。 – Sumurai8 2015-02-07 19:32:15

回答

1

它可以通過AJAX完成。簡而言之,AJAX是一種允許在前端觸發事件時向後端發送請求的技術。

你可以做如下:

在HTML:

<!-- Change myTabId to whatever id you want to send to the server side --> 
<element onclick="loadTab(myTabId)">my tab</element> 

在您的JS:

// Will be executed on tab click 
function loadTab(tabId) { 
    var xmlhttp = new XMLHttpRequest(); 
    // Define a handler for what to do when a reply arrives from the server 
    // This function will not be executed on tab click 
    xmlhttp.onreadystatechange = function() { 
     // What to do with server response goes inside this if block 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      // Change the content of the element with id "myTabContent" to the server reply 
      document.getElementById("myTabContent").innerHTML = xmlhttp.responseText; 
     } 
    } 
    // Opens a connection to "myServerSideScript.php" on the server 
    xmlhttp.open("GET", "myServerSideScript.php?id=" + tabId, true); 
    xmlhttp.send(); 
} 

現在,你需要在服務器根目錄中創建myServerSideScript.php類似內容以下內容:

$id = $GET[id]; //The GET parameter we sent with AJAX 
$query3 = $db->query("SELECT * FROM mybb_game WHERE id='" . $id . "'"); 
$response = ""; 
while ($row = mysqli_fetch_array($query3)){ 
    $response .= $row[name]; 
} 
// To return a reply you just need to print it 
// And it will be assigned to xmlhttp.responseText on the client side 
echo $response; 

您可以瞭解更多關於AJAX的信息here