Reload a page with a tag

I have the following doubt:

Let's say I have a page in php at this localhost/mipagina address.php and another that is in localhost / mipagina2.php and I need to put a tag <a href=""> that reloads me the same page by putting a variable in the url.

However that <a> tag is in a header "template" that is the same for all pages.

I.e. if I put <a href="localhost/mipagina2.php"> and I'm ON PAGE 2 I reload it but if I'm on mipagina1.php not the reload, if not that takes me to the 2 and I want it to just reload me the same but with a variable in the url.

I edit to better explain what I want to do, it's something like this although obviously this is wrong but I'm very new to jQuery and stuff...

<a href="#" onclick="myFunction()">Añadir parametro par=2 en la url</p>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "<?php $_GET['par'] = '2';?>";
}
</script>
 4
Author: Pavlo B., 2016-10-18

6 answers

You can use a relative url, such that like this:

 <a href=".">Recargar</a>

And you don't need any logic on the server to generate it. However, to do this using PHP, you could use the following code:

 $pagina_actual = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

This however has the drawback that the client can modify the value of both variables at Will, causing unexpected results.

There is a possibility to do this using javascript:

 <a href="javascript:window.location.href=window.location.href">Recargar</a>

The advantage of the latter is that in in case the URL contains parameters, it keeps them.

To generate it from PHP, keeping the parameters sent by get, you could do something like this:

 <?php
 // Construir la base de nuestra url
 $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]?" . http_build_query($_GET);

 // Comprobar si el parámetro par viene en la url
 if (!isset($_GET['par']) || trim($_GET['par']) == '') {
     $url .= '&par=2';
 }
 ?>
 <a href="<?=$url?>">Recargar</a>

This code is for sample purposes only, as it can be highly optimized in terms of performance and security, but this will give you a clear idea of how to do it.

 8
Author: Muriano, 2016-10-19 10:21:47

At least in Chrome, location has a property search that allows you to do exactly what you need:

<a href="#" onclick="location.search='?foo=bar'">Reload</a>

This way you reload the page by adding the variable foo with the value bar to the URL. I don't know if it works in other browsers.

Here is a complete example for browsers that do not support the search Property and for cases where previous parameters already exist:

<html>
   <head>
      <meta charset="utf-8">
      <script>
         function refresh() {
            var url = location.href.replace(/#$/, '');
            if (/\?/.test(url)) {
               var partes = url.split('?');
               var params = partes[1].split('&');
               var paramsfinal = [];
               for (var i in params) {
                  if (!params[i].startsWith('foo=')) {
                     paramsfinal.push(params[i]);
                  }
               }
               paramsfinal.push('foo=bar');
               location.href = partes[0] +'?' +paramsfinal.join('&'); 
            } else {
               location.href = url +'?foo=bar';
            }
         }
      </script>
   </head>
   <body>
      <p>foo es <?php echo isset($_GET['foo']) ? $_GET['foo'] : '(vacío)'; ?></p>
      <a href="#" onclick="refresh()">Recargar</a>
   </body>
</html>
 3
Author: Juan Manuel, 2016-10-20 14:43:05

You could use PHP's function basename() to get the file name and point your link to that file according to where you are at the time, something like this:

<?php
  echo '<a href="'.basename(__FILE__).'">Recargar</a>'; 
?>

Or you could also use javascript to reload.

 1
Author: Juan Pinzón, 2016-10-18 16:47:21

I think the best option is with:

window.location.reload(true)

Using it this way:

<a href="#" onclick="window.location.reload(true)">

Example

document.getElementById("time").innerHTML = Date();
<html>
<body bgcolor="aqua">

<a title="Reload!" href="#" onclick="window.location.reload(true)">Reload !</a>
	
<p id="time"></p>

</body>
</html>
 1
Author: Jorgesys, 2016-10-18 17:12:26

Another option that has not been mentioned, is simply with $_SERVER ['PHP_SELF']:

<a href="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">Recargar</a>

Security Info:

When you insert any variable in HTML (as in this case of the URL), and can be modified by a user, it is highly recommended that you use the function htmlspecialchars(), prevents such an attack Cross-Site-Scripting (XSS).

 0
Author: Black Sheep, 2016-10-18 20:26:13

Another similar option is

location.reload();

Can also be:

location.reload(true);

The difference is that when you put true you download the page again from the server, this is mainly useful in IE since in some versions when occupying this did not refresh the page.

 0
Author: Ariel Navarrete, 2016-10-19 02:44:35