2010-10-28 171 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 
} 
相关问题