Active Tab with Prototype and Ajax
I assume some basic knowledge on HTML/CSS, PHP and Javascript.
Problem:
Creating a Tab navigation menu with an active state menu item with javascript and update corresponding content width Ajax.
Here is a screenshot of the final solution:

You can see the final solution here.
Solution:
- Part 1
- Create the following file structure:
- index.php - main file;
- main.css - to style everything;
- protoype.js - download Prototype library here;
- content.php - the file with the content that will be used to update some div in index.php
- Open your index.php file and add tho following code in the body:
-
-
<div id="wrap">
-
<div id="mainNav">
-
<ul>
-
<!– set the default active menu item to "active" and others to "inactive –>
-
<li><a id="event1" class="active" href="#">Tab 1</a></li>
-
<li><a id="event2" class="inactive" href="#">Tab 2</a></li>
-
<li><a id="event3" class="inactive" href="#">Tab 3</a></li>
-
</ul>
-
</div>
-
<div id="content"></div>
-
</div>
-
Take your time to look at this structure to understand it.
Basically you have two divs. One will be the main navigation, and the other, the content.
Why the content div is empty? Well the contents of this div will be in content.php and will be inserted here with Ajax. -
- Now, open your content.php and insert the following code:
-
-
<?php
-
if($_GET[‘menu’] == ‘1′) {
-
echo "<h1>Content 1</h1>
-
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
-
Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque,
-
id pulvinar odio lorem non turpis. Nullam sit amet enim.</p>";
-
}
-
if($_GET[‘menu’] == ‘2′) {
-
echo "<h1>Content 2</h1>
-
<p>Vivamus pharetra posuere sapien.
-
Nam consectetuer. Sed aliquam, nunc eget euismod ullamcorper, lectus nunc ullamcorper orci,
-
fermentum bibendum enim nibh eget ipsum.
-
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
-
Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque,
-
id pulvinar odio lorem non turpis. Nullam sit amet enim. Suspendisse
-
id velit vitae ligula volutpat condimentum.</p>";
-
}
-
if($_GET[‘menu’] == ‘3′) {
-
echo "<h1>Content 3</h1>
-
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
-
Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque,
-
id pulvinar odio lorem non turpis.</p>
-
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
-
Morbi commodo, ipsum sed pharetra gravida, orci magna rhoncus neque,
-
id pulvinar odio lorem non turpis. Nullam sit amet enim. Suspendisse
-
id velit vitae ligula volutpat condimentum. Aliquam erat volutpat.
-
Sed quis velit. Nulla facilisi. Nulla libero.</p>";
-
}
-
?>
-
-

Content.php has 3 different sections. It displays different content according to the parameter in the URL (menu=1, 2 or 3). You probably already gessed why do we have this three options. According to the Tab you select, content div will be updated with content.php?menu=1, content.php?menu=2 or content.php?menu=3.
Now that you have your index.php and your content.php ready I’ll have to give you a small explanation about the Ajax.Updater function.
-
new Ajax.Updater(container, url[, options])
As you can see, when you call the Ajax.Updater function, it receives 2 (3 optional) parameters:
- container: some div;
- url: url to the file that will be inserted inside the container specified in the first parameter;
- [options]: aditional options.
You can see a complete list of the options available here.
Example:
-
-
new Ajax.Updater($(‘content’), ‘./products.php’, { method: ‘get’ , parameters:"item="+"1" });
-
In this example, the div with id=”content” will be updated with products.php?item=1.
In this second part you’ll use the Ajax.Updater function to insert the contents of content.php inside the div with id=”content”.
- Part II
- The first thing you have to do is to link the Prototype library inside your index.php. To do this insert the following code between the “head” tags:
-
<script type="text/javascript" src="./prototype.js"></script>
-
- Now lets add some javascript between “script” tags:
- open script tag:
-
<script type=’text/javascript’>
-
- Call Event.observe function in order to run the code AFTER the window loads:
-
-
<script type=‘text/javascript’>
-
//as soon as page loads
-
Event.observe(window,‘load’, function(event) {
-
//call Ajax.Updater and update default content
-
new Ajax.Updater(‘content’,‘./content.php’, { method:‘get’, parameters:"menu="+"1" });
-
//Add listenners to each one of the Tab links
-
Event.observe($(‘event1′), ‘click’, function(event) {
-
//if click on link with id="event1"
-
//set the class of "event1" to "active"
-
$(‘event1′).className = ‘active’;
-
//set other link’s classNames to "inactive"
-
$(‘event2′).className = ‘inactive’;
-
$(‘event3′).className = ‘inactive’;
-
//call the Ajax.Updater function to update the contents
-
//inside <div id="content"> with content.php?menu=1
-
new Ajax.Updater(‘content’, ‘./content.php’, { method: ‘get’ , parameters:"menu="+"1"});
-
//stop Event.observe
-
Event.stop(event);
-
});
-
//Same for the other two links
-
Event.observe($(‘event2′), ‘click’, function(event) {
-
$(‘event1′).className = ‘inactive’;
-
$(‘event2′).className = ‘active’;
-
$(‘event3′).className = ‘inactive’;
-
new Ajax.Updater(‘content’, ‘./content.php’, { method: ‘get’ , parameters:"menu="+"2"});
-
Event.stop(event);
-
});
-
Event.observe($(‘event3′), ‘click’, function(event) {
-
$(‘event1′).className = ‘inactive’;
-
$(‘event2′).className = ‘inactive’;
-
$(‘event3′).className = ‘active’;
-
new Ajax.Updater(‘content’, ‘./content.php’, { method: ‘get’ , parameters:"menu="+"3"});
-
Event.stop(event);
-
});
-
//stop first Event.observe
-
Event.stop(event);
-
});
-
</script>
-
-
- open script tag:
Ok, last step, style everything according to your preferences using the “main.css” file (don’t forget to link this file to your index.php file). Now you have the classes “.active” and “.inactive” to style the different tags.
If you have any doubts, comments or suggestions, fell free to post a comment on this article.
Regards,
Tiago






