0

Possible Duplicates:
How to obtain anchor part of URL after # in php
Is it possible to access anchors in a querystring via PHP?

i need to find a way to do this, facebook does it so it has to be possible....

If a user goes to this URL in the browser
http://localhost/index.php?photos=p#12345

Then I can have my PHP load a photo with the ID 12345 from mysql

If a user went to
http://localhost/index.php?photos=p#123456 Then it would load a photo with id 123456

I just need help in getting the value in the URL after the # and accessing it with PHP if possible? IF it is not possible, then I maybe I can access it with jQuery and then make an AJAX call to load an image based on this value.

So does anyone know how I can get that value?

Community
  • 1
  • 1
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • Duplicate: http://stackoverflow.com/questions/484113/is-it-possible-to-access-anchors-in-a-querystring-via-php and http://stackoverflow.com/questions/1032242/how-to-obtain-anchor-part-of-url-after-in-php – Dominic Rodger Jan 15 '10 at 15:15

6 Answers6

2

You can get the hash in Javascript with

document.location.hash
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
1

You can only access this value with Javascript as it is never sent to the server, so PHP never gets it. As Kenny said, you can access it with

document.location.hash

or

window.location.hash

but you cannot access it with PHP.

Facebook is a very heavy Javascript site actually. You probably want to do something like send an AJAX request for picture/comment data immediately when the page is loaded. Then when someone presses the 'next' button, you do another request.

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
1

The way applications do this is parse the hash using javascript, using document.location.hash, and dynamically loading the image using AJAX or another client-side technique. This AJAX call can post the hash to a PHP script that does any necessary server-side processing and returns an image url or something similar.

If you use the dom:loaded event many browsers or javascript frameworks support, this happens almost seamlessly.

Kamiel Wanrooij
  • 12,164
  • 6
  • 37
  • 43
0

These values (the anchor) are usually not send to the server by browsers. So you can't depend on them.

Decent Dabbler
  • 22,532
  • 8
  • 74
  • 106
0

It's available in JavaScript as window.location.hash. This is a dupe of another SO question that has some answers.

Community
  • 1
  • 1
Tom
  • 22,301
  • 5
  • 63
  • 96
0

The value after the hash identifies a point in the document. It isn't part of the identity of the document itself. So a URL like "/path/to/something#section" will cause the browser to request "/path/to/something" from the server, then jump/scroll to the point identified by "section."

You want to build the photo ID into the URL or query string:

http://localhost/index.php?photoId=12345

Willis Blackburn
  • 8,068
  • 19
  • 36