Can the PHP $_GET be used to get a variable in the URL using Hashchange?

544 Views Asked by At
<a href="#create=1">Click Me</a>
<script src="https://raw.github.com/cowboy/jquery-hashchange/v1.3/jquery.ba-          hashchange.js"></script>
<script type="text/javascript">
    $(window).hashchange(function () {
    //
    });
</script>

When Click Me is clicked the URL looks like this "www.mydomain.com/#create=1".

What I am trying to do is us the $_GET in PHP to bring down the parameter. Ex:

<?php echo $_GET['create'];?>

Using the

<a href="?create=1">Click Me</a>

works, but it reloads the page and that is what I am trying to avoid. Any help would be greatly appreciated.

3

There are 3 best solutions below

2
On BEST ANSWER

There is a new future for that:

window.history.pushState("Remember me!", "Changing the get Parameter...", "?create=1");

You can apply this to all links by using this:

$("a").click(function() {

    if ($(this).attr("href").substr(0, 1) != "#") {
        $("body").load($(this).attr("href"));
        window.history.pushState("Remember me!", "Nothing special", $(this).attr("href"));
        return false;
    }
});
1
On

PHP runs on the server. A request has to be sent to the server for PHP to know what is in the query string. You don't need to reload the whole page but you will need to send something to the server, e.g. in an AJAX request and do something with the result.

1
On

//java script code

$("#clickme").click(function(event) {
    var arr = $(this).attr('href').split('#');
    var arr=(arr[1]);
    event.preventDefault();
    $("#content").load("data.php?"+ arr);   
});

//html code 

<a id="clickme" href="#create=1">Click Me</a>  
<div id="content"></div>

// php code

data.php