anchor tags and php
November 12, 2006 2:12 PM   Subscribe

I'm having trouble getting anchor tags working in a php-generated page.

Everything is php-generated (no hard-coded HTML). Inside each div, the php generates this code:

[a href="676"]

And that seems to be working fine.

Trouble is, I have other links on the page that link to these anchors and also send querystring vars, i.e.

[a href="page.php?x=1&y=2&z=3#676"]blah[/a]

Here's the exact code that's generating the hyperink:

print "[a href=\"index.php?rank={$rplus}&id={$row['id']}{$params}#{$row['id']}\"](+1)[/a]";

The vars are all references to stuff pulled from the db.

Everything seems to be working fine, except the page isn't scrolling to the anchor.

If it helps, the page is here (sorry about the self-link). The links are for the ranking system. (I know I could rank with AJAX. This is just meant to be a quick fix -- let you rank by resubmitting the page and then return you to where you were.) I thought this would take me two minutes to implement, but an hour and many failed google-searches later it's still not working.
posted by grumblebee to Computers & Internet (11 answers total) 1 user marked this as a favorite
Best answer: You want to generate [a name="676"], not [a href="676"]
posted by null terminated at 2:17 PM on November 12, 2006

Response by poster: slaps forehead! Thanks, null.
posted by grumblebee at 2:20 PM on November 12, 2006

Response by poster: This is a perfect example of not practicing what I preach. I teach programming, and I always tell my students to examine ALL parts of a broken system. Yet I only looked at the hyperlink part. I assumed the named anchors were fine. If I'd eyeballed them for ten seconds, I would have noticed my dumb error.
posted by grumblebee at 2:24 PM on November 12, 2006

nitpick: since you're DTD says XHTML Strict, you need to escape those ampersands in the query. That is,

[a href="page.php?x=1&y=2&z=3#676"]blah[/a]
posted by sbutler at 2:33 PM on November 12, 2006

Response by poster: Thanks, sbutler. I will.
posted by grumblebee at 2:36 PM on November 12, 2006

Best answer: You can also use the ID attribute of any element (including <A>) as the destination of an #anchor. So for example if you had a <H2> that you wanted to be an anchor for a link just give it an id.
posted by Rhomboid at 2:36 PM on November 12, 2006

Response by poster: Rhomboid, is that a safe technique to use for most in-the-wild browsers?
posted by grumblebee at 2:40 PM on November 12, 2006

actually, modern thinking is that "id"s should be used for anchors, and "names" be used within forms.
posted by MonkeySaltedNuts at 2:46 PM on November 12, 2006

Is that a safe technique to use for most in-the-wild browsers?

Sure is.
posted by Khalad at 3:04 PM on November 12, 2006

Given that this is part of the HTML 4.01 standard (which is now close to 7 years old) I would darn well hope that any browser found in-the-wild would support it.
posted by Rhomboid at 3:22 PM on November 12, 2006

Bit late to the game on this thread, but "id" not "a name" is fine on all browsers. It was a little bit flaky on IE6 running on Windows NT, for some reason. Here's the pertinent part of the XHTML spec on this: Fragment identifiers
posted by TheDonF at 11:32 AM on November 17, 2006

« Older Wooosh!   |   Flash 8 / Actionscript 2.0 – How do I draw... Newer »
This thread is closed to new comments.