views:

1513

answers:

3

I am using a jquery tabbed interface here http://www.imashdigital.com/#2 and would like to return the tab number in php.

Ideally I would like to run a javascript function (on a timer) that continually updates a global php variable with the current tab.

Based on this php value, 1 through to 4, I will then load a different sidebar.

I would be grateful for any help and some code examples as I am a novice.

Kind regards

Jonathan

+1  A: 

I would suggest you do not run a timer but instead attach the $.post to the event "tab activation". This will make any tab change applied in real time and it won't trigger needless requests.

PepperBob
A: 

The part of an URI that comes after the hash is never sent to the server. There is no way that PHP can access it. Use a querystring parameter ($_GET) instead. Or use client side scripting (javascript).

troelskn
uhm, not sure if you are the one that downvoted me, but the hash goes through just fine using my method
Paolo Bergantino
Client side scripting is actually what has been suggested in Paolo's example, isn't it?
PepperBob
PepperBob: I meant client side scripting alone. Paolo's example stores the state at the server side. I guess the real question is why Jonathan wants to know the selected tab in PHP?
troelskn
A: 

I have used tabbed panels in a couple recent projects, and the solution I've used is the following:

HTML

<ul class="tabs">
  <li><a href="#en_en">English</a></li>
  <li><a href="#fr_fr">Français</a></li>
</ul>
<div class="panel" id="en_en"><!-- Content --></div>
<div class="panel" id="fr_fr"><!-- Content --></div>

jQuery

// the currently selected tab, or a default tab (don't forget to prepend the #)
var tab = location.hash || '#en_en';

// register a click handler on all the tabs
$('ul.tabs a').click(function(event){
    event.preventDefault(); // prevents the browser from scrolling to the anchor

    // hide all panels, then use the link's href attribute to find 
    // the matching panel, and make it visible
    // you can, of course, use whatever animation you like
    $('div.panel').hide().filter(  $(this).attr('href')  ).show();

).filter('[href*='+tab+']').click();
// above: in case of refreshing/bookmarking: find the tab link that contains
// the current location.hash, and fire its click handler

It works well because the server-side code doesn't need to know which tab is selected, but it also supports refreshing or bookmarking a specific tab without requiring the user select the tab again.

Daniel Wright