Jump to content

Call javascript function and redirection as onclick values of div?


adredz

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.