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);
});
});
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>
Take a look of parse_url
http://php.net/manual/en/function.parse-url.php
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)