1

I have written a single page application in HTML5. I use JQuery and hash navigation to navigate between pages. This works fine so far. Now I have written a really simple php script that pulls blog entries from my database. Of course I want the user to be able to link those articles and navigate tags etc. How would I implement that?

I tried http://example.com/#blog?tag=news but somehow my php script is unable to catch that parameter.

BadSnowflake
  • 256
  • 1
  • 2
  • 12

5 Answers5

2

That is a fragment denoted by the # and is only used by the browser and not passed to the server. PHP will not receive $_GET['tag'] and not #blog as you have it constructed.

Try ?tag=news#blog but PHP will still not get the #blog.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
2

do it like http://www.example.com?tag=news#blog in my opinion it looks nicer and also the # as @AbraCadaver stated is only used by the browser.

<?php echo $_GET['tag']; ?> //would output news
Martin E.
  • 267
  • 1
  • 12
  • yes, using a hash before a query string will not work. This is the only way. – Seth Mar 10 '14 at 20:24
  • also worth noting that if you need to have both variables you could use http://www.example.com?tag=news&page=blog where echo $_GET['page'] would be blog – Martin E. Mar 10 '14 at 20:34
1

you can catch using this var $_GET['tag']

Icaro Martins
  • 307
  • 12
  • 22
  • The best Answer is change the order like [AbraCadaver](http://stackoverflow.com/a/22310490/2456894) said, but you can get the path and work it manually like in this link http://stackoverflow.com/questions/16198790/get-url-path-in-php – Icaro Martins Mar 10 '14 at 20:34
0

you must use java scritp eg:

var hash = location.hash;
it will return the hash value to you
but you will need send to server-side using
the post or get of java script

0

I have solved the problem and wanted to share here how.

The underlying problem here was the loading of all content with JQuery. While my initial URI was wrong, http://example.com/?tag=news#blog did not solve it either. The variable was never sent to the $_GET Array of PHP, because the index.html does not contain any PHP and the script is loaded into a div via JQuery/Ajax.

So the solution here was to modify my JQuery Function:

function navigate(theHash)
{
    var parsed = theHash;
    var tag    = '';

    if ( theHash.indexOf("/") != -1 ){ /* Check if there is a trailing slash in the string */
        parsed = theHash.substr(0, theHash.indexOf("/"));
    }
    if ( theHash.indexOf(",") != -1 ){ /* Check for Parameters to send to PHP */
        parsed = theHash.substr(0, theHash.indexOf(","));
        tag = theHash.substr(theHash.indexOf(','), theHash.length);
    }


    if ( parsed.localeCompare("#!portfolio") == 0 ){
        $('#content').load('portfolio.html');
    }
    else if ( parsed.localeCompare("#!blog") == 0 ){
        if ( tag.localeCompare('') != 0 )
        {
            $("#content").load('blog.html?tag='+tag.substr(1, tag.length));
        }
        else{
            $("#content").load('blog.html');        
        }
    }
    else{
        $('#content').load('front.html');
    }
}

So with the little change in the blog part of the if statement to load the html page with the parameter appended (which contains the php code), the variable gets transported correctly to PHP and I can read it via $_GET. While this is only a first draft and needs to get refined for more parameters and more robust behavior, it shows how I solved the issue.

BadSnowflake
  • 256
  • 1
  • 2
  • 12