Reputation: 16173
I have two separate forms on the index.php page:
<form action="search_results.php" method="get" id="search_housing_area">
<input type="text" name="rent_housing" />
<inout type="submit" name="search_housing" value="" />
</form>
<form action="search_results.php" method="get" id="search_rooms_area">
<input type="text" name="rent_rooms" />
<inout type="submit" name="search_rooms" value="" />
</form>
When I submit either of those forms the url appears as:
http://www.domain.com/search_results.php?rent_housing=1234&search_housing=
OR
http://www.domain.com/search_results.php?rent_rooms=1234&search_rooms=
On the search_results.php page, I have two divs, #housing_results and #rooms_results. They are both hidden by default. If the GET variable 'search_housing' exists, I would like to show div#housing_results and if the the GET variable 'search_rooms' exists, I would like to show div#rooms_results.
How can I make a specific div show using jQuery if a specific GET variable exists in the url?
Upvotes: 0
Views: 4819
Reputation: 4539
<?php
$disp_div=0;
if(isset($_GET['search_housing']))
{
$disp_div=1;
}
else if(isset($_GET['search_rooms']))
{
$disp_div=2;
}
?>
Jquery code
$(document).ready(function(){
var show=<?php echo $disp_div; ?>;
if(show==1)
{
$('#div_search_housing').show();
}
else if(show==2)
{
$('#div_search_rooms').show();
}
});
Upvotes: 6
Reputation: 2414
You can use pure JS:
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
from here
So in your case it should be like this:
if (getParameterByName('search_housing').length > 0) { ... }
if (getParameterByName('search_rooms').length > 0) { ... }
Upvotes: 0
Reputation: 50797
You can use window.location
and search the query string
.
var winLoc = window.location.search;
//will output ?rent_rooms=1234&search_rooms=
Now we'll remove the ?, split the strings on the &
symbol, and make it an array
winLoc = winLoc.replace('?', '').split('&');
var ourStr = winLoc[1].replace('=', '');
switch(ourStr){
case 'search_rooms':
$('.switch').css('backgroundColor', 'red');
break;
case 'search_housing':
$('.switch').css('backgroundColor', 'blue');
break;
}
Here is a working jsFiddle with a predefined string, for examples sake
Upvotes: 2
Reputation: 9055
Here's a pure JS solution. There are also ways to do this using PHP, as mentioned in the other answers.
function stuff()
{
var queryPairs = window.location.href.split('?').pop().split('&');
for (var i = 0; i < queryPairs.length; i++)
{
var pair = queryPairs[i].split('=');
if (pair[0] == 'search_housing')
{
$('#rooms_results').hide();
$('#housing_results').show();
return;
}
if (pair[0] == 'search_rooms')
{
$('#housing_results').hide();
$('#rooms_results').show();
return;
}
}
// None of the two options exist in the query
}
Upvotes: 1