Search code examples
phpvariablesparameter-passingurlvariables

how to pass variables in url with an ID section using PHP?


I can do this:

<a href="home.php?var=home">home</a>

But when I have a link with an Id, I can't pass variables:

<a href="home.php#sectionID?var=home">home</a> <--!this doesn't work of course -->

Thanks a lot!


Solution

  • Use window.location.hash in javascript

    <script>alert(window.location.hash);</script>
    

    And the parse_url() function in PHP

    <?php echo parse_url("home.php?var=home#sectionID",PHP_URL_FRAGMENT);?>
    

    your HTML will be..

    <a href="home.php?var=home#sectionID">home</a>