Get JS values for PHP

Asked

Viewed 158 times

1

I have this code. However it is in JS and would like to play the values for a var in PHP and work better with the 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 that? Ex.

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

http://www.localizaip.com.br/api_localiza_ip.php

  • @Danielomine did not find answer for this in the link above!

  • I need to take these values because the way this one is showing me an Alert with the data, I just want to show in a variable so I can manipulate this data into chiasquer shapes!

  • @Danielomine I understood, there is no simpler method just to store the values in a var?!

  • Where it comes from ip_data and how it is invoked LocalizaIP_done() ?

  • @Danielomine is an API that generates a script only, I can’t say :/

  • @Danielomine see the API link for better intender

Show 1 more comment

2 answers

0

Javascript code must be inside the .php. file Make sure to keep this code out of the php tag ( )

Ai, try to do the following inside the javascript code:

var cidade = <?php print ip_data['city'] ?>;

-1

How php is in server you need to pass the variables by query string or post.

For that you must make one 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 ... 

Get in PHP with $_POST, $_GET or $_REQUEST.

But if the question is to facilitate the front-end, you can use some template engine, example Twig.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.