Get JS Variable value and pass to PHP and send by URL

0

I have the following code below. I want to know if there is a way to send through the URL a variable that was passed from JS to PHP. The code intension is to pass the variable after clicking the button.

NOTE: The user will be directed to this page described in the link

<html>
   <head>
   
  <title>Passar Variável Javascript para PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <scripttype="text/javascript">
     var variaveljs = 'Mauricio Programador';
  </script>
   </head>

  <body>
   
  <?php
     $nome="<script>
     $(function() {
         $('#botao').click(function () {

             document.write(variaveljs);
         });
     });</script>";


       ?>

  <div id="botao">
    <?php
     echo " <a href='pagina2.php?valor='.$nome>Botão</a>";
     ?>
   </body>
</html>
    
asked by anonymous 23.05.2018 / 16:39

1 answer

1

You do not even need PHP for this. If the value is already on the page that will link, just use JS only.

Simple example:

const variavel = "SOpt";
const link = document.getElementById('link');

link.href += variavel;
<a id="link" href="pagina.php?valor=">Link</a>                                    
23.05.2018 / 16:59