I have a really simple example of a form that's split into tabs that it is posted onto itself for verification and processing with php, simplified version of code is as below (without the verification and processing steps):
<style>
/* Style the list */
ul.tab {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
border: 1px solid #ccc;
background-color: #f1f1f1;
}
/* Float the list items side by side */
ul.tab li {float: left;}
/* Style the links inside the list items */
ul.tab li a {
color: black;
display: inline-block;
font-size: 15px;
font-weight: lighter;
padding: 14px 16px;
text-align: center;
text-decoration: none;
transition: all 0.3s ease 0s;
}
/* Change background color of links on hover */
ul.tab li a:hover {background-color: #ddd;}
/* Create an active/current tablink class */
ul.tab li a:focus, .active {background-color: #ccc;}
/* Style the tab content */
.tabcontent {
display: none;
padding: 60px 10px;
border: 1px solid #ccc;
border-top: none;
margin-bottom: 20px;
}
</style>
<ul class="tab">
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_1')">tab 1</a></li>
<li><a href="#" class="tablinks" onclick="openTab(event, 'tab_2')">tab 2</a></li>
</ul>
<!-- FORM Starts -->
<form method="POST" action="">
<div id="tab_1" class="tabcontent">
Tab One Content
</div>
<div id="tab_2" class="tabcontent">
Tab Two Content
</div>
<input type="submit" name="save_settings_button" value="Save Settings" />
</form>
<script>
function openTab(evt, tabName) {
var i, tabcontent, tablinks;
tabcontent = document.getElementsByClassName("tabcontent");
for (i = 0; i < tabcontent.length; i++) {
tabcontent[i].style.display = "none";
}
tablinks = document.getElementsByClassName("tablinks");
for (i = 0; i < tabcontent.length; i++) {
tablinks[i].classList.remove("active");
}
document.getElementById(tabName).style.display = "block";
evt.currentTarget.classList.add("active");
}
var mybtn = document.getElementsByClassName("tablinks")[0];
mybtn.click();
</script>
This helps to break up big forms into sections however if a user in on tab 2 and presses the submit button they are taken back to tab one, not a very good user experience!
Is there any way to use javascript to recognize which tab is in focus when the form is submitted and then reload the page on that tab once the page is reloaded?