Page 1 of 1
named anchors in dynamic content
Posted: Tue May 23, 2006 11:05 am
by vasilis
In a project of mine I produce a dynamic page (product_list.php) with a list of products taken from a database. When I click on one of these products I get to another page which has info for this specific product (product_info.php) with a "Return" link on the bottom of the page. When I click on this "Return" link, I get the previous product list page but the browser gets me to the top of it. I want to add code so the "Return" link gets me to the same spot of the product list page where the clicked product is. With static html pages I know that you can do that with named anchors (using "id=..." for an element) and with "#". How can I do the same with php pages and dynamically created links?
Thanks for any help
Posted: Tue May 23, 2006 4:47 pm
by timvw
You can easily redirect the users to previous.php#bottom too...
But the problem is that (afaik) you can't store the location the user was surfing at... Eg: it's impossible to determine if a user is at #top or at #bottom when he follows a link..
Posted: Wed May 24, 2006 4:36 am
by vasilis
but, that's exactly what my problem is... How do I redirect my users to the previous.php and at the specific link which they clicked? It seems that I can't use any named anchors in dynamically produced pages.
Posted: Wed May 24, 2006 4:46 am
by neogeek
I set up a quick example of how this can be done. Hope it does the trick.
Code: Select all
<?php
if (!isset($_GET['use_id'])) { print('<a href="redirect.php?use_id=test">Start Test</a>'); }
else { print('<a href="redirect.php#' . $_GET['use_id'] . '">Return</a>'); }
?>
Posted: Wed May 24, 2006 4:58 am
by GM
Can't you do something like:
Code: Select all
<a name="$id_product"></a><a href="product_info.php?id_product=$id_product">Go to Product Detail</a>
and then, from the product_info.php page, on your return link have:
Code: Select all
<a href="product_list.php#$id_product">Return</a>
Posted: Wed May 24, 2006 5:09 am
by neogeek
@GM: Exactly, my reason for coding my example the way I did was to allow for a one file testing script. When implementing the script, they way you set it up was perfect.
Posted: Wed May 24, 2006 5:16 am
by GM
In fact... looking again at my code, I'm thinking that it'd would work just as well with:
Code: Select all
<a name="$id_product" href="product_info.php?id_product=$id_product">Go to Product Info</a>
and then in product_info.php:
Code: Select all
<a href="product_list.php#$id_product">Return</a>
It is the "NAME" attribute on the anchor tag that allows you to go to that point in the document.
Note - I haven't tested any of this, just going on gut feeling.
Posted: Fri May 26, 2006 7:00 am
by vasilis
I have tried the above code but nothing works. When I use the '#', e.g.
Code: Select all
<a href=product_list.php#$product_id?var1=$var1&var2=$var2>Return</a>
,
then when I get in the 'product_list.php' script in debugging mode (been using the Zend Development Environment), I get null values in the variables of the query string, e.g. $var1, $var2 are null therefore my code doesnt work correctly, something that doesnt happen when I erase the '#$product_id' part (but then I return to my initial code which redirects my users to the top of the page...), so I think that the '#<spot in the page>' patent doesnt work with php files. I hope I am wrong....
Posted: Fri May 26, 2006 7:19 am
by GM
You need to put the # as the last thing in the URL, so
Code: Select all
<a href="product_list.php?var1=$var1&var2=$var2#$product_id">Return</a>
Posted: Wed May 31, 2006 2:54 am
by vasilis
I did it the way you say and it works fine! Thanks