How to obtain anchor part of URL after # in php

后端 未结 4 1360
花落未央
花落未央 2020-12-02 01:13

While using LightBox mechanism in my project I got an URL http://nhs/search-panel.php#?patientid=2 I need to collect that patientid from this through GET mechanism, Is that

相关标签:
4条回答
  • 2020-12-02 01:43

    Simply put: you can't! Browsers don't send the fragment (the part of the URL after the hashmark) in their requests to the server. You must rely on some client-side javascript: perhaps you can rewrite the url before using it.

    0 讨论(0)
  • 2020-12-02 01:45

    Maybe everybody else is right and a simple $_GET is enough but if the # in your URL ( http://nhs/search-panel.php#?patientid=2 ) is supposed to be there you would have to do that with JavaScript (and Ajax e.g. JQuery) because everything after # is not included in the request as far as I know.

    0 讨论(0)
  • 2020-12-02 02:01

    If you check your server logs, you should see that no browser actually transmits the #anchor part of the URL the request, so you can't pick it up on the server side.

    If you need to know it, you'll need to write some Javascript to extract it from the document.location.href and send it to your server, either by turning it into a regular GET parameter and redirecting the user, or in the background with an XMLHttpRequest/AJAX.

    0 讨论(0)
  • 2020-12-02 02:06

    Edit: Whoops, this won't work. The other posters are correct in saying that anything after the hash never reaches your server.

    Something along these lines should do you:

    //Get complete URI, will contain data after the hash
    $uri = $_SERVER['REQUEST_URI'];
    
    //Just get the stuff after the hash
    list(,$hash) = explode('#', $uri);
    
    //Parse the value into array (will put value in $query)
    parse_str($hash, $query);
    
    var_dump($query);
    
    0 讨论(0)
提交回复
热议问题