how can I show a javascrip variable and store it

0

How can I show in php the variables that javascript prints?

I want to show these variables in php:

var x=e.latlng.lat;
var y=e.latlng.lng;
window[e.type].innerHTML = "Lat: " + x + " Log: " +  y + " contador: "+cont ;

I'm trying like this but it does not give me:

<?php 
     $latitude ="<script>document.write(cont);</script>";
     echo "latitude =$latitude";
?>
    
asked by miguel 19.11.2018 в 17:23
source

1 answer

0

You can not assign a javascript value to php that way.

I quote another user's response

  

PHP runs on the server side, and JS on the client side. The   sequence would be more or less like this:

     

-The client requests the server to view a page .php

     

-The server executes the PHP code to generate a plain text file. This plain text will normally be HTML, which may contain   JS code between "script" tags

     

-The server sends the generated text and the client receives it (the client will normally be a browser, such as firefox or chrome)

     

-The client will interpret the HTML and will show it properly, also executing the javascript code that it contains, but it is executed   IN THE CUSTOMER

     

As you can see, the execution of the PHP code has already finished by   complete in step 2, but the execution of the JS code is done in   step 4 and on a different machine (the PHP has been executed in the   server, the JS has been executed on the client).

link

What you can do is send the javascript value through ajax to process it in a php, and if what you need is to print the value so nothing else, you simply select the element and add the value:

document.getElementById("myText").value = latitude;
    
answered by 19.11.2018 в 17:38