How to pass a javascript variable to a php variable?

0

What I want is for my php variable to get the value of my javascript variable

So I get my javascript value:     

    (function() {

var variable="contenido";
alert(variable);

})();

</script>

So I want to pass it to my php variable:

$url = "<script> document.write(variable) </script>";
echo 'variablePHP = '.$url;
    
asked by David 22.03.2018 в 01:21
source

1 answer

3

The problem occurs because your variable is not global, so php can not find it, you must declare your variable globally. I share an example of passing values through cookies.

<script> 
var variable = "";
(function() {

this.variable="contenido";
alert(this.variable);

})();

/*Ejemplo mediante cookiees*/
document.cookie = "lastname=Martinez";
</script>

<?php
$url = "<script> document.write(variable) </script>";
echo 'variablePHP = '.$url;

/*Ejemplo mediante cookies*/
$last_name = $_COOKIE['lastname'];
echo "Apellido".$last_name;
?>
    
answered by 22.03.2018 / 06:50
source