Getting hash from url in php

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
Posts: 199
Location: Holland Rotterdam

Getting hash from url in php

I`m back again found a solution on the question before thanks for that
|My new problem I try to detect # hash with a id

like this url example but it does not skip to the page part ????


I tried two things in my document

Code: Select all

<div id="'.$row["comment_id"].'">

Code: Select all

<a name="'.$row["comment_id"].'">#'.$row["comment_id"].'</a>

You cannot get the #hash part from url in php, because that value isn't send to server.
It can be used in javascript with: window.location.hash

could you give any example because i try for 5 hours now without any succes
It works if i put the numbes #123 manual after page load but it not works on page load

Do you want to put the #hash in javascript or in php?
What did you try?
In php you cannot get the #hash from current page url, unless it is send with javascript via ajax.

In js code you can get the #hash from current page address like this:

Code: Select all

// For example, url is: //localhost/page.php#123
var url_hsh = window.location.hash;

alert(url_hsh);  // #123

I tried some options mostly if not working I delete it
but what |I try I cant get after page load skip
to the has-id in php or with a javascript

Ow yes I do not known anything about javascript
if I do something it is lucky-shots

It must be something like this

I saw also many articles about hash but did not saw any
url with .php?id111#111 or something like

Code: Select all

window.location.hash = hashTag;

Code: Select all


$(document).ready(function() {
 var hash = window.location.hash;
 if (hash) {
  var elem = document.getElementById(hash.substring(1));
  if (elem) {

This script I saw also what working but not on page load with url but only when page is loaded

Code: Select all


body { 
  font: 14px/2 Georgia, serif; 

#page-wrap { 
  max-width: 500px; 
  margin: 1rem auto;
  padding: 1rem;

h1, h2 {
  line-height: 1.2;

p, ul, h1 { 
  margin: 0 0 1rem 0; 



// Select all links with hashes
  // Remove links that don't actually link to anything
  .click(function(event) {
    // On-page links
    if (
      location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') 
      location.hostname == this.hostname
    ) {
      // Figure out element to scroll to
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
      // Does a scroll target exist?
      if (target.length) {
        // Only prevent default if animation is actually gonna happen
        $('html, body').animate({
          scrollTop: target.offset().top
        }, 1000, function() {
          // Callback after animation
          // Must change focus!
          var $target = $(target);
          if ($":focus")) { // Checking if the target was focused
            return false;
          } else {
            $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable
            $target.focus(); // Set focus again


<a name="/#'.$row['comment_id'].'" href="#'.$row['comment_id'].'"><button>skip</button></a>
& this script works but also not after page load with url

Code: Select all

<button onclick="myFunction1()">top</button> 

function myFunction1() {
  location.href = "#top";

<a id="top"></a>

I tested it again &nd it seems like to work now `thanks for supporting`