duanraa1984 2013-03-05 04:50
浏览 40

在编码的URL中获取预定义变量

I am trying to get my predefined variable $_GET['loc'] Can anyone help me ? The problem is I don't want to disable my anchor tag for ajax using javascript. So what i did is I just added a hash-tag (#) on my href. The URL looks like this

http://localhost/test/onspecial/index.php#filter?loc=dn 

What I need to attain is to access the $_GET['loc']. I really appreciate any help.

my href attribute looks like this

<a href="#filter?loc=dn"></a>

here is my full navbar in index.php and wanted to get the value of $_GET['loc'] for my other queries :

<div class="navbar">
  <ul class="nav" id="nav">
    <li><a href="index.php">home</a></li>
    <li><a href="index.php">about us</a></li>
    <li><a href="index.php">contact us</a></li>
    <li class="dropdown">
      <a id="drop_tog"href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="1000" data-close-others="false">Location</a>
      <ul class="dopdown-menu" id="loc">
        <li id="loc1"><a tabindex="-1" href="#filter?loc=dc" >City</a></li>
        <li id="loc1"><a tabindex="-1" href="#filter?loc=ds" >South</a></li>
        <li id="loc1"><a tabindex="-1" href="#filter?loc=dn" >North</a></li>
      </ul>
    </li>

  </ul>
</div>

javascript looks like this sending request on getresult.php :

$(document).ready(function(e) {
    function getLoc(param){
    //filter url
    var encode = param.substring(7);
     if(window.XMLHttpRequest){
     xmlhttp = new XMLHttpRequest();
     }
     else{
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
     }

     xmlhttp.onreadystatechange = function(){
      if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
      document.getElementById("li_start").innerHTML = xmlhttp.responseText;
      }
     }
     xmlhttp.open("GET","getresult.php"+encode,false);
     xmlhttp.send();
     }

    //handle anchor clicks
    $("ul#location li a").click(function(){
        var loc = $(this).attr("href");
        getLoc(loc);
    });


});
  • 写回答

2条回答 默认 最新

  • dongtan5558 2013-03-05 04:59
    关注

    Change :

    http://localhost/test/onspecial/index.php#filter?loc=dn
    

    To:

    http://localhost/test/onspecial/index.php?loc=dn#filter
    

    Basically, (almost) everything after a ? in a URL is the "query string" until a # is reached. Then, (almost) everything after the # becomes an "anchor".

    The ? must come before the #.

    Edit based on extra info and HTML.

    I don't use jQuery, which it looks to me like you are using. Because of that, I re-wrote the javascript and a small part of your HTML in the following sample. In you HTML, you do not have a HTML tag with an id of li_start, but I assume that is somewhere else in your HTML.

    The Javascript:

    <script type="text/javascript">
    function linkClick(id) {
        page('li_start','/getresult.php?loc='+ id);
    }
    function loadPage(http, id, url){
        if((http.readyState == 4) && (http.status == 200 || window.location.href.indexOf("http") == -1)) {
            document.getElementById(id).innerHTML = http.responseText;
        }
    }
    function page(id, url){
        var http = false;
        var bustCacheParam = (url.indexOf("?")!=-1)? "&"+ new Date().getTime() : "?"+ new Date().getTime();
        if (window.XMLHttpRequest) {
            http = new XMLHttpRequest()
        } else if (window.ActiveXObject){
            try {
                http = new ActiveXObject("Msxml2.XMLHTTP")
            } catch (e) {
                try {
                    http = new ActiveXObject("Microsoft.XMLHTTP")
                } catch (e) {}
            }
        } else {
            return false;
        }
        http.open('GET', url+bustCacheParam, true);
        http.onreadystatechange = function(){ loadPage(http, id, url); }
        http.send(null);
    }
    </script>
    

    And the changed HTML:

    <ul class="dopdown-menu" id="loc">
        <li id="loc1"><a tabindex="-1" href="#" onclick="linkClick('dc');return false">City</a></li>
        <li id="loc2"><a tabindex="-1" href="#" onclick="linkClick('ds');return false">South</a></li>
        <li id="loc3"><a tabindex="-1" href="#" onclick="linkClick('dn');return false">North</a></li>
    </ul>
    
    评论

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?