2

live at http://vivavidadesign.com/includes/content.php

I have the PHP code:

<?php
if (isset($_GET["#photo"])){
    print ("HELLOOOOOOO");    
} else {
    print ("Nothing Set");
}
?>

I am using this file in to return files in conjunction with AJAX.

Previously going to http://vivavidadesign.com/includes/content.php#photo would trigger (isset($_GET["#photo"])). I can trigger it with ?photo, but I want to know how to trigger using a hash i.e. #photo

6
  • 3
    It won't be triggered since your browser will nor send anchor to your server (and after '#' is an anchor) Commented Jan 4, 2014 at 18:40
  • Don't tell me that you are using id value, you need to use the name value, oh I just saw the link, it's just a fragment identifier, you need something like content.php?get_this=photo Commented Jan 4, 2014 at 18:40
  • I want to trigger an event in PHP using URL hash Commented Jan 4, 2014 at 18:41
  • You can't use $_GET with #. You have to use ?. Commented Jan 4, 2014 at 18:42
  • @MElliott it is working with ? but I wanted to do it using # somehow Commented Jan 4, 2014 at 18:43

7 Answers 7

1

The browser is never sending hash to the server, when requesting data.

You have to send it manually, ie. using jquery:

var hash = window.location.hash;
$.ajax({ url: 'content.php?photo=' + hash});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I have changed the var url to "/includes/content.php?" + window.location.hash.substring(1); in AJAX in order to send the data, and strip the hash.
1

PHP does not grab a hash / anchor. Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

You would have to parse it using Javascript and possibly send via ajax to your php script.

Comments

0

#xxx at the end of URL is not argument to the page but anchor in the page. So PHP doesn't put it in GET variable.

Either use [code] http://vivavidadesign.com/includes/content.php?%23photo [/code]

if you really need the name of variable to be #photo or you can pass variable in the classic wat like this [code] http://vivavidadesign.com/includes/content.php?photo=something [/code]

Comments

0
<?php

if(isset($_GET["photo"])) {
    echo $_GET["photo"];    
} else {
    echo "Nothing Set";
}

?>

And call it:

 index.php?photo=file.jpg

Btw. do not use print if it is not needed, use echo instead.

Comments

0

How to get the value after the hash in "somepage.php#name"?

A get is included in the URL after a questionmark, eg.
http://www.example.com/?photo=1
An anchor is included in the URL after a hashtag, eg.
http://www.example.com/#photo
An anchor is never sent to the server from the browser, but you might be able to find it using javascript and through javascript pass it on to the server.

If you want your variable to be a get you need to change the $_GET['#photo'] into $_GET['photo'] in your code and #photo into ?photo=something in your URL.

Comments

0

You have to choose one php or javascript to get value :

  1. PHP

    $ar = explode("#",$_SERVER['REQUEST_URI']);
    print_r($ar[1]);

Demo : https://eval.in/85871

2 JS:

var hash = window.location.hash;  
document.location.href = '/content.php?photo='+hash;

Comments

0

This is similar to this other question.

You do this not with $_GET, but with parse_url().

The part you are looking for is the fragment

Array
(
    [path] => includes/content.php
    [fragment] => photo
)

Edit to clarify:

If this URL is coming from the client, you're misusing/misunderstanding how the hashtag functions in a URL: http://en.wikipedia.org/wiki/Fragment_identifier . You must parse it client side via javascript.

Comments