By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,967 Members | 815 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,967 IT Pros & Developers. It's quick & easy.

how to pass variable to a second tab

100+
P: 123
i have a page which is home.php on that page i have a CSS tab which is tab1 and tab2 on that page in both tabs i have a link called next that pass variable to the next page (<a href='home.php?page=$lastpage'>NEXT</a>).if i click on next in the tab1 on home page(home.php) it will pass the variable and bring me to the home page and tab1 if i click on the next in tab2 it will also bring me to home page and take me to tab1 back.i want it to bring meto the home page and tab2 how can i do that.

Expand|Select|Wrap|Line Numbers
  1.  
  2. <html>
  3. <head>
  4. <title></title>
  5. <link href="SpryTabbedPanels.css" rel="stylesheet" type="text/css" />
  6.  
  7. <?php
  8. if (isset($_GET['page'])) {
  9. $pages = ($_GET['page']);
  10. } else {
  11. echo "page one";
  12. }
  13. ?>
  14.  
  15. <ul class="TabbedPanelsTabGroup">
  16. <li class="TabbedPanelsTab" tabindex="0">Tab1</li>
  17. <li class="TabbedPanelsTab" tabindex="0">Tab2</li>
  18. </ul>
  19.  
  20. <div class="TabbedPanelsContent">
  21. <div class="tabb">
  22. <?php
  23. echo "<a href='home.php?page=$lastpage'>NEXT</a>";
  24. ?>
  25. </div></div>
  26.  
  27. <div class="TabbedPanelsContent">
  28. <div class="tab">
  29. <?php
  30. echo "<a href='home.php?page=$lastpage'>NEXT</a>";
  31. ?>
  32. </div></div>
  33.  
  34. </head>
  35. </html>
  36.  
Oct 9 '09 #1

✓ answered by kovik

If you have these tabs inside of one page, I'd recommend doing one of two things:
  1. Using the query string and $_GET, or
  2. Using JavaScript to display the tabs.

Session data is for preserving data across page requests. What you are making IS a page request, and does not represent data that should persist across requests. What you want to do is, basically, just make your tabs into links. Plain and simple.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $active_tab = isset($_GET['tab']) ? (int)$_GET['tab'] : 0;
  3. $tabs = array(
  4.   1 => 'Tab 1',
  5.   2 => 'Tab 2',
  6. );
  7. ?>
  8. <ul id="tabs>
  9.   <?php foreach ($tabs as $tab_id => $tab_name): ?>
  10.   <li<?php if ($active_tab == $tab_id) echo ' class="active"'; ?>>
  11.     <a href="?tab=<?php echo $tab_id; ?>"><?php echo $tab_name; ?></a>
  12.   </li>
  13.   <?php endforeach; ?>
  14. </ul>
  15. <div id="tab-content">
  16. <?php if ($active_tab == 1): ?>
  17. This content is in tab #1.
  18. <?php elseif ($active_tab == 2): ?>
  19. This content is in tab #2.
  20. <?php else: ?>
  21. The tab that you have selected is invalid. Sneaky, sneaky.
  22. <?php endif; ?>
  23. </div>
Then, you'd style the tabs with CSS and add whatever other logic you want.

Share this Question
Share on Google+
12 Replies


Dormilich
Expert Mod 5K+
P: 8,639
er, could you rephrase your question please, I didnít get what you want.
Oct 9 '09 #2

100+
P: 123
i want tab2 to display if i click on the link Next in tab2 it should not take me back to tab1
Oct 10 '09 #3

TheServant
Expert 100+
P: 1,168
So you need to track which tab you're on. Either making a $_SESSION variable to include current_tab or something, or using $_POST / $_GET...

I would go with $_SESSION so you don't have to submit to move pages ($_POST) and you don'r have to change URLs ($_GET).
Oct 11 '09 #4

100+
P: 123
@TheServant
how can i track the tab and also make a $_SESSION variable to include current_tab
Jul 13 '10 #5

TheServant
Expert 100+
P: 1,168
@simon2x1
You should read up on $_SESSION's, but they are very similar to other variables. You could also use $_COOKIE's which will be able to remember which tab they're on even if they close the browser and come back in a week.
Expand|Select|Wrap|Line Numbers
  1. // To set a session variable:
  2. $_SESSION['current_tab'] = "About Tab";
  3. // To recall a session variable:
  4. if ($_SESSION['current_tab'] == $current_tab_name) {
  5. echo "selected";
  6. }
  7.  
That conditional statement can obviously be anything and you would use it to show a css property, or call a javascript function or however you're tracking which tab to display.

Also when you're using sessions you need to tell PHP you're going to be using sessions on every page. You need to put this line at the top of every page that will use the $_SESSION super variable:
Expand|Select|Wrap|Line Numbers
  1. session_start();
Jul 13 '10 #6

100+
P: 123
i still did not get it well help me take a look at my code below and tell me what the problem his
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. session_start();
  5. $_SESSION['TabbedPanelsTab'] = "About Tab";
  6. if ($_SESSION['TabbedPanelsTab'] == $current_tab_name) { 
  7. echo "selected"; 
  8. }   
  9. ?>
  10.  
  11. <html> 
  12. <head> 
  13. <title></title> 
  14. <link href="SpryTabbedPanels.css" rel="stylesheet" type="text/css" /> 
  15.  
  16. <?php 
  17. if (isset($_GET['page'])) { 
  18. $pages = ($_GET['page']); 
  19. } else { 
  20. echo "page one"; 
  21. ?> 
  22.  
  23. <ul class="TabbedPanelsTabGroup"> 
  24. <li class="TabbedPanelsTab" tabindex="0">Tab1</li> 
  25. <li class="TabbedPanelsTab" tabindex="0">Tab2</li> 
  26. </ul> 
  27.  
  28. <div class="TabbedPanelsContent"> 
  29. <div class="tabb"> 
  30. <?php 
  31. echo "<a href='home.php?page=$lastpage'>NEXT</a>"; 
  32. ?> 
  33. </div></div> 
  34.  
  35. <div class="TabbedPanelsContent"> 
  36. <div class="tab"> 
  37. <?php 
  38. echo "<a href='home.php?page=$lastpage'>NEXT</a>"; 
  39. ?> 
  40. </div></div> 
  41.  
  42. </head> 
  43. </html> 
  44.  
  45.  
  46.  
Jul 21 '10 #7

TheServant
Expert 100+
P: 1,168
Where do you get your $current_tab_name? There is no definition there?

To get into more specifics, the question is how do you intend on changing tabs? Will your tabs be different pages, or will all content be loaded and then tabs only show selected content. The second option is more user friendly, but will require javascript, and will not be great for SEO and page tracking.

If you are reloading every time a tab is changed, maybe session variables will not be ideal and a nicer $_GET variable will be easier, as you probably don't want to start doing AJAX functions between changing pages... Maybe you do? If it's just changing tabs, a $_GET should be fine, and with some .htaccess tricks, you can make pretty URL's.
Jul 22 '10 #8

100+
P: 123
the tabs are on the same page not different pages am talking about the content of the tabs i want the content of tab2 to display when ever i click submit button in the content of tab2 not to take me back to tab1.
Oct 2 '10 #9

Dormilich
Expert Mod 5K+
P: 8,639
the submit button does not take you back to tab1. it just loads the page with the default setting, which is probably tab1.
Oct 2 '10 #10

kovik
Expert 100+
P: 1,044
If you have these tabs inside of one page, I'd recommend doing one of two things:
  1. Using the query string and $_GET, or
  2. Using JavaScript to display the tabs.

Session data is for preserving data across page requests. What you are making IS a page request, and does not represent data that should persist across requests. What you want to do is, basically, just make your tabs into links. Plain and simple.


Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $active_tab = isset($_GET['tab']) ? (int)$_GET['tab'] : 0;
  3. $tabs = array(
  4.   1 => 'Tab 1',
  5.   2 => 'Tab 2',
  6. );
  7. ?>
  8. <ul id="tabs>
  9.   <?php foreach ($tabs as $tab_id => $tab_name): ?>
  10.   <li<?php if ($active_tab == $tab_id) echo ' class="active"'; ?>>
  11.     <a href="?tab=<?php echo $tab_id; ?>"><?php echo $tab_name; ?></a>
  12.   </li>
  13.   <?php endforeach; ?>
  14. </ul>
  15. <div id="tab-content">
  16. <?php if ($active_tab == 1): ?>
  17. This content is in tab #1.
  18. <?php elseif ($active_tab == 2): ?>
  19. This content is in tab #2.
  20. <?php else: ?>
  21. The tab that you have selected is invalid. Sneaky, sneaky.
  22. <?php endif; ?>
  23. </div>
Then, you'd style the tabs with CSS and add whatever other logic you want.
Oct 4 '10 #11

100+
P: 123
i want to thank you for your reply i will appreciate it if i will be able to use JavaScript to solve this problem using this php code make me more confess please i need the java script version that can solve this problem
Oct 23 '10 #12

kovik
Expert 100+
P: 1,044
I'm sorry, but if you can't do this yourself, chances are trying to explain how to use my code would go over your head anyway. Good luck, though.
Oct 25 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.