adredz Posted December 3, 2009 Share Posted December 3, 2009 Is it possible to call a javascript function and at the same time redirect to another page with "onclick" attribute of <div?? Does it make sense to call javascript function at all in this case since all its variables will be reverted to the default values once the page gets refreshed(redirected) right? Then how can I set a variable that holds a value that's accessible from one page to another? PHP and MySQL? Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 Then how can I set a variable that holds a value that's accessible from one page to another? You can use php sessions. Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 adredz, what exactly are you trying to do? It sounds like you are trying to post values from one page to another, you can do this by either using an html form posting to a php page or if you really want to use javascript and you do not want the page to redirect, you can use Ajax. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 I have a tabbed content box on another page and on my homepage are thumbnails that are related to the contents of the tabbed box. What I'd like to happen is that, when I click one of those thumbnails I get redirected to the page with tabbed content box and set the default active tab where the clicked thumbnail belongs... Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 I think PHP sessions is what I need... Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 Using Sessions for keeping track of what page you're on isn't the most optimal way to it, but I guess it will work. Quote Link to comment Share on other sites More sharing options...
trq Posted December 3, 2009 Share Posted December 3, 2009 Using Sessions for keeping track of what page you're on isn't the most optimal way to it, but I guess it will work. What is the most optimal way then my good friend? Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 Care to explain Tazz, and probably show some code? I'd like to know too.. Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 If I understand you correctly, you have a list of thumbnails representing pages on your website. Once clicked, you want the tab to remain highlighted/selected/whatever? The most optimal way to do it would obviously depend on the framework you are using. Say you are using an MVC framework and have defined three view files. Header, body and footer. Your thumbnail will have an href pointing to the controller of ther file eg. You'll obviously have a few of these: <a href="www.example.com/bla"><img src="NOT_SELECTED.jpg"/></a> Ok, so you have a controller named "bla". This controller will process any data and send it to the Views. Controller "bla" will pass an extra variable named $selected_page to your header file. In this case tthe value of $selected_page will be "bla" So now your header page will have the following layout for the thumbnails: <a href="www.example.com/bla"><img src="<?php ($selected_page == 'bla' ? 'SELECTED.jpg' : 'NOT_SELECTED.jpg') ?>"/></a> Sessions are there to store session specific info, such as username, or id. You can obviously use it for whatever you want, but programmers sometime forget to unset them after use or they get lost when the session times out. Rather take the safe route. http://www.sqlmag.com/Articles/Print.cfm?ArticleID=49114 Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 My point is, why store a variable in a Session, when you can just pass it to wherever you need it, and solve your problem. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 I think I now know how to make it through sessions. But the problem is how can redirect and set the session value at the same time? I tried this <div id="nothing" onclick="<?php echo $_SESSION['tab']='3'; ?>"> <a href="www.nothing.com"></a></div> but it won't work.. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 Thanks tazz, digesting your post.. Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 Hey adredz I dont know for a fact if your code will work. But what I would suggest trying is : <div id="nothing" onclick="<?php $_SESSION['tab']='3'; ?>"> <a href="www.nothing.com"></a></div> Remove the echo in the onclick event. Also, have you started the session on that specific page? Just add this above your code session_start(); You have to insert that on every page you intend to access session variables. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 Tazz I think you misunderstood me(or maybe I misunderstood). The communication isn't about pages. Let me make it clear, I have a page that contains a table with tabs representing the categories. Each tab has ID. Say I have a Car category and under which are honda, toyota, ford. In my homepage are thumbnails(e.g honda) that are selected randomly from all categories. Now when the honda thumbnail gets clicked in the homepage, I want the onclick attribute to send information to my javascript function which would set the default active tab/category to, in this case, Car... I hope it's clear now... Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 Have you already written that javascript function that sets your active/default tab? Does it take a paramater which is the ID of the tab? Then it's simple. You have the js: <script> function setTabs(tabID) { //set my tab here } </script> And the thumbnail: <div id="nothing" onclick="setTabs('nothing')"> <a href="www.nothing.com"></a></div> Quote Link to comment Share on other sites More sharing options...
syed Posted December 3, 2009 Share Posted December 3, 2009 I think someone here said you dont need to use sessions and I agree. You could pass a variable through the query string and on page load you can evaluate which tab needs to be selected. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 Tazz, won't work. It's because page gets refreshed before javascript can access the the div/tab... The sessions way won't work too... is this how you access the value of the session: <div class="<?php if ($_SESSION['tab']==3){ echo "active"; }?>"></div> Is it correct? Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 sorry double post.. Quote Link to comment Share on other sites More sharing options...
Tazz Posted December 3, 2009 Share Posted December 3, 2009 Im with syed on this one, lets try this rather: <div id="nothing"> <a href="www.nothing.com?tab=3"></a></div> Note the the ?tab=3 added to the and of the URL and then on the page where you display the thumbnails: <div class="<?php if ($_GET['tab']==3){ echo "active"; }?>"></div> I like to use shorthand if notation let me show you how : <div class="<?php echo ($_GET['tab']==3 ? 'active' : '')?>"></div> Both should work, just personal preference which way you choose. Quote Link to comment Share on other sites More sharing options...
adredz Posted December 3, 2009 Author Share Posted December 3, 2009 lol I wanna shout for joy! you just saved my day Tazz! Thank you! works like a charm... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.