2010-10-28 65 views
0

我有一個頁面,home.php。在頁面上,我有CSS選項卡tab1tab2。這些標籤在同一頁面上不是不同的頁面。我想要tab2的內容顯示,只要我點擊tab2的內容中的提交按鈕,不要把我帶回tab1。我如何使用JavaScript來實現這種行爲?如何在單擊按鈕時顯示標籤的內容

<html> 
<head> 
<title>home</title> 
</head> 
<link href="SpryTabbedPanels.css" rel="stylesheet" type="text/css" /> 
<body> 
<ul class="TabbedPanelsTabGroup"> 
<li class="TabbedPanelsTab" tabindex="0">Tab1</li> 
<li class="TabbedPanelsTab" tabindex="0">Tab2</li> 
</ul> 

<div class="TabbedPanelsContentGroup"> 

<div class="TabbedPanelsContent"> 
//this is where the content of the tab1 will be and i have another form in this content too 

</div> 

<div class="TabbedPanelsContent"> 
//this is where the content of the tab2 will be 
<form action="home.php" method="post"> 
<?php 
if (isset($_POST['submit'])) { 
if(empty($_POST['boded']){ 
echo"Please do it again."; 
}else{ 
$mgs = ($_POST['boded']); 
//then insert to my database 
} 
?> 
<textarea id="message" name="boded" rows="5" cols="40"></textarea> 
<input type="submit" name="submit" value="Submit" /> 
//if i click this button(submit) in this content it should display tab2 content not tab1 
</form> 
</div> 

</div> 
</body> 
</html> 

我試過第一個人問我要做什麼;它的工作原理,但有一個問題:提交按鈕並沒有發送到$_POST['submit']是否因爲我的按鈕(<input type="submit" name="submit" value="Submit" onClick="show_content('div_2'); return false;"/>)中的假我秋天我提到在tab2內容我有一個PHP代碼,接收按鈕發送給它。

以上是我重新編輯的代碼,如果您在編碼上編輯,我將非常感激。

+2

請修復您的代碼粘貼(縮進4空格或使用ctrl + k選項) – drudge 2010-10-28 18:52:34

+1

另外,請爲您的問題添加一些標點符號和大寫字母。我很確定那裏肯定有不止一句話。 – kindall 2010-10-28 19:08:17

回答

0

您是否確實已將表單提交給Web服務器並重新發送給您頁面信息,或者是提交按鈕只是運行JavaScript函數而您仍然保持在同一頁面上?

如果您正在重新加載頁面內容,那麼當您單擊提交時,您需要向服務器發送之前所用的選項卡,以便在發送新頁面時進行調整以更改默認選項卡。這可能涉及將「當前」類移至不同的選項卡,或將display: none;放在原始「起始選項卡」上,將display: block;放在新的選項卡上。

有兩種方法可以做到這一點。如果每個標籤有一個表單,則只需在表單中輸入<input type="hidden" name="tab" value="2" />。通過這種方式,當您將表單與其他值一起提交時,它會提醒服務器您正在使用哪個選項卡。但是,如果您只有一個表單可以延伸到每個選項卡,則這不起作用。在這種情況下,您將提交隱藏的輸入,並且由於它們具有相同的名稱,通常只發送最後一個名稱。

如果您有多個表單,那麼解決方法是更改​​您的<input type="submit" />按鈕的值或名稱。它實際上可以檢查這個值,就像任何其他的輸入,所以在服務器端(在PHP中,在這個例子中),你可以做以下之一:

(changed name of input per tab): 
if(isset($_POST["submit_tab2"]){ 
    // They were on tab 2 
} 

(changed value): 
if($_POST["submit"] == "Click here to submit tab 2!"){ 
    // They were on tab 2 
} 
相關問題