Forum Moderators: coopster

Message Too Old, No Replies

Accessing an anchor's id attribute via php

         

neophyte

2:10 pm on May 28, 2015 (gmt 0)

10+ Year Member



Hello All -

Having a quick issue that I can't figure out.

I'm using an anchor tag in a project with an id attribute like so:

<a id="fr-fr" href="sample">test</a>

To test that I can get the "fr-fr" value from the attribute I also have:

if(isset($_REQUEST['id'])) {
echo 'ID attr = ' , $_REQUEST['id'];
} else {
echo 'ID is not set';
}

Interestingly, every time I click that link I always get "ID is not set".

By the way, I've got mod-rewrite set up in my .htaccess file for this project but I didn't think that should affect getting the anchors ID value.

Any advice on how I can get this working would be greatly appreciated.

Wittner

2:35 pm on May 28, 2015 (gmt 0)

10+ Year Member



I think you need to put your id here:

<a href="sample?id=fr-fr">test</a>

Then, on your receiving page, you can get the id from the request.... unless I'm missing something... which is very possible! ;-)

cheers,

Wittner

[edited by: Wittner at 2:40 pm (utc) on May 28, 2015]

whitespace

2:37 pm on May 28, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



What are you trying to do exactly?

That PHP snippet is "way off"... you could use a bit of code like that if you were submitting an HTML form and you had a form element whose NAME attribute had the value "id". But anchors aren't submitted with form data in any shape or form.

EDIT: Or you are trying to do something like what Wittner says. But that's not an ID attribute, that's a URL (GET) parameter.

lucy24

6:03 pm on May 28, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<tangent>
id="fr-fr"
I hope you just said that because you were typing fast and had to say something. There's no law that says you can't use "fr-fr" as an id. But it's such an obvious language form, you're bound to end up confusing yourself if you use it as an id. (And if you actually are talking about alternate links with various language attributes, that would be an entirely different structure, not using id at all.)
</tangent>

neophyte

11:53 pm on May 28, 2015 (gmt 0)

10+ Year Member



Hi Wittner, Whitespace and Lucy24 -

Thank you all for your responses.

As Lucy noted, what I'm trying to do is use this "hidden" <a> attribute to trigger a change in site language; this is accomplished in my application by setting/re-setting a $_SESSION variable.

I've already got this working using "href="sample?id=fr-fr" and $_GET but I was interested in testing out another possibility where the attribute didn't show up in the URI.

Last night I came across a comment from another forum site that noted that one could populate a $_SESSION var using an anchor tag's "name" attribute and $_REQUEST['name']. I never thought of that before and thought I'd give it a try.

Since the "name" attribute is now deprecated for anchor tags in HTML5, I was trying this with "id" instead.

So - in essence - that's the the scenario I'm trying to accomplish: populate a $_SESSION using a value contained in a "hidden" anchor var rather than "href="sample?id=fr-fr" and $_GET.

Any way to get this done?

All comments/guidance greatly appreciated.

lucy24

1:08 am on May 29, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yikes. One absolutely crucial difference between name and id is that an id can only occur once per page. Not once for an anchor, once for a div, once for a paragraph and so on. Once, period.
sample?id=fr-fr
and
a id = "fr-fr"
are entirely different and unrelated things. The first form is an "id" parameter whose value happens to be "fr-fr". The second form is an HTML attribute pertaining to some element on a page.

HTML5 doesn't like "name" on anchors, true, but it's still perfectly happy with "name" on some other elements. For example a group of radio buttons could all have the same name, or a selection of table cells could all have the same name. (Both of those would be used in scripting-- and in general, once you can do something in javascript, there will be a way to do it in php*.) Now, if you could attach your shared "fr-fr" name to some other element, such as a span containing the <a> or vice versa...


* Since I speak three words of php as against six words of javascript, I try this fairly often. I have now learned, for example, that "blahblah.length" is not valid php syntax ;)

neophyte

1:40 am on May 29, 2015 (gmt 0)

10+ Year Member



Hi Lucy -

Thanks for your reply.

I'm aware that an element's id can only be used once per page and, indeed, the language string (id="fr-fr" for example) would only be used on a single anchor on any given page.

As mentioned, I've already got this working without issue using $_GET, but am interested to see if I can use Php to grab the id attribute of a link and use it for the same purpose

So, the essence of my question is now: what php function can I use (if any) to grab the id attribute when a link is clicked?

Perhaps someone else can answer that question.

Thanks for weighing in.

Wittner

8:25 am on May 29, 2015 (gmt 0)

10+ Year Member



So, if I get the user story correctly, a user clicks a link on a preceding page via an anchor tag which looks like this:

<h2>European legislation on car parks</h2>
<a href="euopean-ligislation-carpark-article.php" id="fr-fr">Take me to the French page</a>
<a href="euopean-ligislation-carpark-article.php" id="gb-gb">Take me to the English page</a>

The user clicks one of the links on this page, and the PHP page accesses the id of the link which has been clicked and processes the request appropriately. Is this correct?

cheers,

Wittner

whitespace

8:46 am on May 29, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Last night I came across a comment from another forum site that noted that one could populate a $_SESSION var using an anchor tag's "name" attribute and $_REQUEST['name'].


There's something missing here, did you actually see a working example? Without perhaps some help from JavaScript, an anchors NAME attribute (or any attribute) is never sent to the server, so you can't use PHP (or any server-side language) to test for it in the request. (?) The only use of the NAME attribute on an anchor that I'm aware of is for internal linking.

...what php function can I use (if any) to grab the id attribute when a link is clicked?


Well, none!? Not with an anchor.

However, you could do this with a standard form (the NAME attribute of any form control element) that is submitted to the server by POST. The value won't appear in the URL and you can easily read this with PHP.

HTML:

<form action="sample" method="post">
<input type="hidden" name="lang" value="fr-fr">
<input type="submit" name="submit" value="test">
</form>

You can make the form "look like" an anchor, using CSS, if you wish.

And then in PHP:

if (isset($_POST['lang'])) {
echo 'lang = ' , $_POST['lang'];
} else {
echo 'lang is not set';
}

neophyte

10:19 am on May 29, 2015 (gmt 0)

10+ Year Member



Wittner -

Yes, the use case you've described is correct.

Whitespace -

As mentioned, I saw an answer to someones post about "populating a $_SESSION VAR through a link" (or some post title that was similar) on a different form site. When I saw that I thought "that's an interesting approach". I did not witness a working example but tried what the answer said which was the genesis of this post.

I have used hidden $_POST attributes for special form processing requirements before which was why I though that maybe anchors could be accessed similarly - obviously I (and the original poster) was incorrect.

Thank you both very much for following this thread but, for the time being at least, I'll be sticking with the $_GET approach rather than getting into a javascript solution which would yield a minimal return for the effort expended.

whitespace

10:42 am on May 29, 2015 (gmt 0)

10+ Year Member Top Contributors Of The Month



Using the form/POST method, as mentioned above, would have the exact same result as the "magical" anchor - it could look and behave the same from a user's perspective.

However, there is the small matter of SEO - if that is a concern. The language identifier would need to be part of the URL for search engines to be able to index the different languages. So, the "$_GET approach" is the better approach IMO.