[html] How to add url parameter to the current url?

Currently I'm at


and I want to make a link to


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

This question is related to html url

The answer is

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

You have to:


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';

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.

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:

function addUrlParameter(name, value) {
  var searchParams = new URLSearchParams(window.location.search)
  searchParams.set(name, value)
  window.location.search = searchParams.toString()

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

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>

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

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

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);

Questions with html tag:

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment Angular 6: saving data to local storage Vue.js get selected option on @change Bootstrap 4 multiselect dropdown How to add bootstrap in angular 6 project? Angular 5 Button Submit On Enter Key Press Angular 5, HTML, boolean on checkbox is checked How to render string with html tags in Angular 4+? bootstrap 4 file input doesn't show the file name How can I execute a python script from an html button? Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width Failed to load resource: the server responded with a status of 404 (Not Found) css Change arrow colors in Bootstraps carousel Bootstrap 4 Dropdown Menu not working? CSS Grid Layout not working in IE11 even with prefixes How to prevent page from reloading after form submit - JQuery Centering in CSS Grid Detecting real time window size changes in Angular 4 Angular 4 img src is not found (change) vs (ngModelChange) in angular Bootstrap 4: Multilevel Dropdown Inside Navigation Align the form to the center in Bootstrap 4 How to style a clicked button in CSS How do I change the font color in an html table? Redirecting to a page after submitting form in HTML Load json from local file with http.get() in angular 2 display: flex not working on Internet Explorer Scroll to element on click in Angular 4 How to extract svg as file from web page force css grid container to fill full screen of device How does the "position: sticky;" property work? HTML5 Video autoplay on iPhone Disable button in angular with two conditions? CSS hide scroll bar, but have element scrollable CSS grid wrapping How to load image (and other assets) in Angular an project? Flask - Calling python function on button OnClick event How can I make Bootstrap 4 columns all the same height? Wrapping a react-router Link in an html button

Questions with url tag:

What is the difference between URL parameters and query strings? Allow Access-Control-Allow-Origin header using HTML5 fetch API File URL "Not allowed to load local resource" in the Internet Browser Slack URL to open a channel from browser Getting absolute URLs using ASP.NET Core How do I load an HTTP URL with App Transport Security enabled in iOS 9? Adding form action in html in laravel React-router urls don't work when refreshing or writing manually URL for public Amazon S3 bucket How can I append a query parameter to an existing URL? How to get domain root url in Laravel 4? How to download image from url Laravel assets url How do I choose the URL for my Spring Boot webapp? How to read html from a url in python 3 ./xx.py: line 1: import: command not found Check if url contains string with JQuery Apache: The requested URL / was not found on this server. Apache How to get base URL in Web API controller? How to get URL parameter using jQuery or plain JavaScript? Given URL is not allowed by the Application configuration Facebook application error how to set image from url for imageView How get value from URL Javascript-Setting background image of a DIV via a function and function parameter Converting Java file:// URL to File(...) path, platform independent, including UNC paths PHP check if url parameter exists Is there a way to pass javascript variables in url? How to change the URL from "localhost" to something else, on a local system using wampserver? How do I open a URL from C++? Escaping ampersand in URL URL rewriting with PHP POST: sending a post request in a url itself Get current URL path in PHP A html space is showing as %2520 instead of %20 How do I get the different parts of a Flask request's url? Convert blob URL to normal URL Removing the fragment identifier from AngularJS urls (# symbol) Get host domain from URL? Remove all special characters from a string Submit HTML form on self page SecurityError: The operation is insecure - window.history.pushState() Passing multiple variables to another page in url Create URL from a String How to specify a local file within html using the file: scheme? how to read xml file from url using php How to launch Safari and open URL from iOS app Curl Command to Repeat URL Request CodeIgniter -> Get current URL relative to base url how to send an array in url request Optional query string parameters in ASP.NET Web API