Paste JS values for PHP [duplicate]

1

I have this code. However it is in JS and I would like to play the values for a var in PHP and work better with Front End.

<script language="javascript">
var LIP_LowPrecision = false; //false = pede permissao pelo navegador, Maior Precisao | true = nao pede permissao, Menor Precisao
function LocalizaIP_done(ip_data){
  if (!ip_data['error']) //esta linha eh um exemplo, deve trocá-la pelo programa que irá manipular os dados de Geolocalizacao
    alert('Localizei IP: '+ip_data['city']+'-'+ip_data['state']+'-'+ip_data['country']+' (lat:'+ip_data['latitude']+',long:'+ip_data['longitude']+')');
}</script> 

How do I do this? Ex.

$cidade = +ip_data['city']+;
  

link

    
asked by anonymous 29.10.2016 / 06:29

2 answers

0

The javascript code must be inside the .php file. Be sure to keep this code out of the php () tag

Alas, try to do the following within the javascript code:

var cidade = <?php print ip_data['city'] ?>;
    
29.10.2016 / 14:24
-1

As php is on the server you need to pass the variables to query string or post .

To do this you must do an HTTP request via URL or AJAX , to facilitate use jQuery .

const postData = {};
postData.city = ...
...
$.post("caminho-do-arquivo.php", postData);

or

const a, b, c;
window.location.href = "arquivo.php?a=" + a + "&b=" + b ... 
                                    
29.10.2016 / 07:01