Links with variables in the URL
The first tool with what we have to remotely define a value to a variable located on the server are the links that send variables.
The first tool we have to remotely define a value to a variable located on the server are the links that send variables.
It is important to clarify that we refer to links that we see in a file that is already on our screen; that is, they are in a "potential" state in an HTML file that was already downloaded to our computer, waiting for us to press one of them to send a data to the server.
When we create a link in HTML, it usually looks like the code seen in the following table (the reader can transcribe it into a complete file with the extension .html):
<a href="destination.html"> This is a link that asks the server for the file called destination.html </a>
The objective of the <a> tag (the a comes from the word anchor -ancla-, that is, an element that "links" us to another page) is to ask the server to deliver the page specified in the href attribute that, in this case, is the destination page.html. Obviously, if we have not located any page called destination.html within the same folder in which this file is located, it will give us an error when we click on the link that requests that resource.
However, in the world of dynamic pages, we can take advantage of the possibility that the links give us to say something to the server, so that, in addition to asking him to show a particular page, we can send a value to some variable that will be available on the server, to be used in the PHP interpreter program a few moments later, while processing the PHP code of that page, just before it returns the generated HTML code to our browser, which waits for a response.
The syntax that allows the sending of variables together with a link is very simple: Suppose we want to define in the server a variable that calls name, whose value will be Pepe, and that the link goes to a page called recib.php. This link - which requests a paginany, at the same time, sends a variable with that value to the server - would be expressed as follows:
<a href="recibe.php?nombre=Pepe"> We request to see the page "recib.php" and in passing we send to the server a variable called "name" containing the value "Pepe" </a>
It is important to note that within the value of the value of the href attribute, after specifying the URL requested (the page receives.php), a question sign has been added: ? This sign indicates that, next, we will send one or more variables to the web server.
The syntax to send a variable is to put its name first; then, an equal sign, then its definition value.
Unlike the syntax used in the PHP language, we must not put any "$" sign before the name of the variable, nor enclose the values of the variables in quotes, even when they are alphanumeric.
This is so since the sending of variables as part of a link, has no relation with PHP or with any other programming language in particular, but it is a possibility that gives us the HTTP protocol, which is what communicates to a browser with a web server. They are not PHP variables until they reach the server and the PHP interpreter program reads them.
In the following example, we will create a file called links.html and another called destination.php
The file links.html will be the following:
<p>
<a href="destination.php?nombre=Pepe">This is Pepe's link</a><br />
<a href="destination.php?nombre=Pedro">This is Pedro's link</a><br />
<a href="destination.php?nombre="Juan">This is the link to Juan </a><br />
</p>
Once our browser displays this page, depending on which of the links we press, we will send to the server a different value for the name variable. We will always send the same variable, but with different information stored inside it. And, it should be noted, that we will only send one of the possible values (there is no mouse that allows us to press more than one link simultaneously).
The file destination.php will assume that variable arrived, for this reason, we will write the print command within the HTML code, which will request the PHP interpreter to write the value of the variable named name.
For that, we will use this syntax:
<php print($_GET["name"]); ?>
Of course, this page lacks the DTD and the opening of the html, head, body, etc. tags, and their corresponding closure, which we must add at the end.
ChatGPT Free
Ask questions on any topic
CITE ARTICLE
For homework, research, thesis, books, magazines, blogs or academic articles
APA Format Reference:
Delgado, Hugo. (2019).
Links with variables in the URL.
Retrieved Dec 10, 2024, from
https://disenowebakus.net/en/links-variables-php