57

Currently I'm at

http://example.com/topic.php?id=14 

and I want to make a link to

http://example.com/topic.php?id=14&like=like 

by not defining the current url. Like <a href="&like=like">Like</a>. However this last one shows me http://example.com/&like=like

1

7 Answers 7

24

There is no way to write a relative URI that preserves the existing query string while adding additional parameters to it.

You have to:

topic.php?id=14&like=like
Sign up to request clarification or add additional context in comments.

4 Comments

There are many ways. See stackoverflow.com/questions/6899097/… for how to using Javascript.
You don't need to repeat the path. Starting with ? is okay. stackoverflow.com/q/7871871/62421
What about using <a href="<?php echo $_SERVER['REQUEST_URI'] . '&filter=2'; ?>"> ?
@rambodrahmani — This isn't a PHP question and that is vulnerable to XSS attacks.
18
function currentUrl() {
    $protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']),'https') === FALSE ? 'http' : 'https';
    $host     = $_SERVER['HTTP_HOST'];
    $script   = $_SERVER['SCRIPT_NAME'];
    $params   = $_SERVER['QUERY_STRING'];

    return $protocol . '://' . $host . $script . '?' . $params;
}

Then add your value with something like;

echo currentUrl().'&value=myVal';

Comments

17

I know I'm late to the game, but you can just do ?id=14&like=like by using http build query as follows:

http_build_query(array_merge($_GET, array("like"=>"like")))

Whatever GET parameters you had will still be there and if like was a parameter before it will be overwritten, otherwise it will be included at the end.

2 Comments

This is php only
You are correct, and I can't believe I didn't realize this question wasn't a PHP question. I just assumed it was because another answer was also given in PHP :/
8

In case you want to add the URL parameter in JavaScript, see this answer. As suggested there, you can use the URLSeachParams API in modern browsers as follows:

<script>
function addUrlParameter(name, value) {
  var searchParams = new URLSearchParams(window.location.search)
  searchParams.set(name, value)
  window.location.search = searchParams.toString()
}
</script>

<body>
...
  <a onclick="addUrlParameter('like', 'like')">Like this page</a>
...
</body>

Comments

6

It is not elegant but possible to do it as one-liner <a> element

<a href onclick="event.preventDefault(); location+='&like=like'">Like</a>

Comments

3

If you wish to use "like" as a parameter your link needs to be:

<a href="/topic.php?like=like">Like</a>

More likely though is that you want:

<a href="/topic.php?id=14&like=like">Like</a>

Comments

1

Maybe you can write a function as follows:

var addParams = function(key, val, url) {
  var arr = url.split('?');
  if(arr.length == 1) {
    return url + '?' + key + '=' + val;
  }
  else if(arr.length == 2) {
    var params = arr[1].split('&');
    var p = {};
    var a = [];
    var strarr = [];
    $.each(params, function(index, element) {
      a = element.split('=');
      p[a[0]] = a[1];
      })
    p[key] = val;
    for(var o in p) {
      strarr.push(o + '=' + p[o]);
    }
    var str = strarr.join('&');
    return(arr[0] + '?' + str);
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.