php - 单击按钮时如何显示选项卡的内容

标签 php javascript html css ajax

我有一个页面,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 代码可以接收发送给它的按钮。

以上是我重新编辑的代码,如果你在make代码上编辑,我将不胜感激。

最佳答案

您是否真的将表单提交给网络服务器并重新向您发送页面信息,或者提交按钮只是运行一个 javascript 函数而您仍然停留在同一页面上?

如果您正在重新加载页面内容,那么当您点击提交时,您需要向服务器发送您之前所在的标签,以便它可以在发送新页面时进行调整以更改默认标签。这可能涉及将“当前”类移动到不同的选项卡或放置 display: none;在原来的“起始选项卡”和display: block;在新的。

有两种方法可以做到这一点。如果每个选项卡有一个表单,则只需要 <input type="hidden" name="tab" value="2" />在您的表格中输入。这样,当您将表单与其他值一起提交时,它会提醒服务器您所在的选项卡。但是,如果您有一个覆盖每个选项卡的单一表单,这将不起作用。在那种情况下,您将提交每个 隐藏的输入,并且由于它们具有相同的名称,通常只发送最后一个。

如果您有多个表单,那么解决方案是更改 <input type="submit" /> 的值或名称按钮。实际上可以像检查任何其他输入一样检查 this 的值,因此在服务器端(在 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
}

关于php - 单击按钮时如何显示选项卡的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4046207/

相关文章:

PHP json_encode,数组在 javascript 中未正确显示

php - 搜索不返回多个关键字的结果

javascript - 谷歌绘制没有数据的饼图

javascript - 如何在 React native 应用程序和网站中实现位置的实时跟踪?

javascript - Backbone js 将单词首字母大写

jquery - 如何在 imageMap 的精确坐标处插入一个 div

php - 无法获取 UTF-8 特殊字符以正确写入 MySQL (PHP)

javascript - Cookie、对象和 JSON

HTML/CSS 我怎样才能在 div 的中心做这个?

html - 如何在一行中使用 perl 的 uri_encode