Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add url parameter to the current url?

Tags:

html

url

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

like image 970
ilhan Avatar asked Dec 19 '11 14:12

ilhan


People also ask

How do I add URL parameters to URL?

Any word after the question mark (?) in a URL is considered to be a parameter which can hold values. The value for the corresponding parameter is given after the symbol "equals" (=). Multiple parameters can be passed through the URL by separating them with multiple "&".

How do I send a parameter in a link?

In order to send the parameters in the URL you donot need to enclose the value inside quote ('') or doublequotes ("") . You just need to send the values (or the multiple values) as shown below.. Also keep in mind that you need to keep track of the urlencoding.

Where is the parameter in URL?

What Are URL Parameters? Also known by the aliases of query strings or URL variables, parameters are the portion of a URL that follows a question mark. They are comprised of a key and a value pair, separated by an equal sign. Multiple parameters can be added to a single page by using an ampersand.


2 Answers

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 
like image 171
Quentin Avatar answered Sep 22 '22 12:09

Quentin


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'; 
like image 37
Marco Pace Avatar answered Sep 21 '22 12:09

Marco Pace