user676853
user676853

Reputation:

GET Multiple Values From Multiple Href

<a href="?cate=politics">Politics</a> ~ <a href="?cout=us">United States</a>

I'm not quiet sure this is possible in PHP, maybe JavaScript but I don't know much JS.

I want to get multiple GET values from multiple a href links. The code is above. Let's say I click on Politics, and then it'll add the Politics value to the URL, and then I'll click on United States, and I'm trying to add the value of United States to the URL too, but at the moment it's just overwriting the one that's there.

If I click on Politics it adds ?cate=politics, and then if I click United States it adds &cout=us as well. So it'll be something like: ?cate=politics&cout=us.

I'm trying to get both GET variables in the URL when clicking both links. If it helps I'm building a filter that will grab the GET variables.

Upvotes: 2

Views: 3126

Answers (4)

Marcin Chwałek
Marcin Chwałek

Reputation: 74

This can't be done in this way. You have two solutions: make hrefs dynamicly on PHP side (tree based structure - IMHO easiest way with using recursion) or make whole "menu" on browser side in JavaScript (I recomend jQuery) - much complicated, "robots" not supported, problems with browsers compatiblity, etc.

Maybe I haven't understood your problem. Maybe you are expecting this solution:

<?php
echo "<a href=\"?cate=politics\">Politics</a>";
$cate = $_GET['cate'];
if ($cate) $cate = "cate=$cate&";
echo "<a href=\"?{$cate}cout=us\">United States</a>";

Upvotes: 0

Ben
Ben

Reputation: 57227

It seems to me the most obvious solution is hard code it into your HTML (use an ampersand & to add multiple parameters to the URL):

<a href="?cate=politics">Politics</a> ~ 
  <a href="?cate=politics&cout=us">United States</a>

Otherwise, you'd need a Javascript event handler like onclick, but if you're new to JS the learning curve might be a bit steep for your immediate needs.

UPDATE (after comment)

In that case, here's a starter function you can work from:

HTML:

<a href='somewhere.html?foo=1' id='test'>Click this link</a>

Javascript:

// $(id) is a common library shortcut to get an element by its id.

$('test').onclick = function() { 
  $('test').href = $('test').href + '&bar=2';
  alert($('test').href);      
}

Not sure what you have in mind after that. window.location.href = ... could be what you're looking for?

An important note though: this way does not degrade gracefully; your form will not work if the user doesn't enable Javascript. Personally, I recommend the first solution of hard-coding the parameters.

Upvotes: 1

Edgar Villegas Alvarado
Edgar Villegas Alvarado

Reputation: 18344

If I understood you, this would be the solution:

<a href="cate=politics" onclick="return addToUrl(this.href);">Politics</a> ~ 
<a href="cout=us" onclick="return addToUrl(this.href);>United States</a>

Javascript:

<script>
function addToUrl(str){
   location.href = location.href + "?" + str;
   return false;
}
</script>

Hope this helps. Cheers

Upvotes: 2

Travis Weston
Travis Weston

Reputation: 924

This is possible in multiple page loads. When you load the page:

$cate = isset($_GET['cate']) ? $_GET['cate'] : null;
$cout = isset($_GET['cout']) ? $_GET['cout'] : null;

echo '<a href="?'.((empty($cate)) ? null : 'cate='.$cate).((empty($cout)) ? null : ((empty($cout)) ? null : '&').$cout).'">Foo</a>';

That "should" do what you are looking for.

Upvotes: 1

Related Questions