Retrieve Jquery variable in PHP and generate cookie

Asked

Viewed 104 times

0

I have this pop-up that, when clicking, will fetch the user’s geolocation:

<div id="modal_success" style="display: none;">
    <div class="modalcontent">
        <div class="modal-header" style="">
            <button type="button" id="button_success" class="close">X</button>
        </div>          

        <?php
            if(isset($_POST['enviar_info']) && $_POST['enviar_info'] == 'env'){
        ?>
            <script type="text/javascript">
                jQuery(document).ready(function($){
                (function() {
                        if(!!navigator.geolocation) {
                            navigator.geolocation.getCurrentPosition(function(position) {

                            var geolocate     = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                            var php_latitude  = position.coords.latitude;
                            var php_longitude = position.coords.longitude;

                            $.post('includes/resp_geolocalizacao.php', {'php_latitude': php_latitude, 'php_longitude': php_longitude}, function(data){});
                        });

                        } else {
                            //document.getElementById('google_canvas').innerHTML = 'No Geolocation Support.';
                        }
                    })();
                }); 
            </script>               
        <?php
            }
        ?>

        <form name="form_login" id="form_login" method="post" class="eltd-login-form_1" enctype="multipart/form-data" action="">
            <fieldset class="corpo_popup">
                Melhore sua localização
                <div class="eltd-login-button-holder" style="padding-top: 26px; text-align: center; border-top: 1px solid #e6e6e6;">
                    <button type="submit" class="eltd-btn eltd-btn-medium eltd-btn-custom-border-hover login_pop_div">   
                        <span class="eltd-btn-text">Clique aqui</span>    
                    </button>   
                    <input type="hidden" name="enviar_info" value="env" />
                </div>
            </fieldset>
        </form>                             
    </div>
</div>

resp_geolocalizacao.php

<?php

/******* Conexão com o bando de dados *******/
include "../Conexao/config.php";
mysqli_select_db($config, $database_config);
mysqli_set_charset($config,"utf8");
/******* Conexão com o bando de dados *******/  

$php_latitude   = $_POST["php_latitude"];
$php_longitude  = $_POST["php_longitude"];
$address        = $php_latitude . "," . $php_longitude;
$geocode        = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');
$output         = json_decode($geocode);
$php_cidade     = $output->results[0]->address_components[2]->long_name;

//print_r($output);

$sql_1 = mysqli_query($config, "SELECT id, uf FROM tb_cidades WHERE nome = '$php_cidade'") or die(mysqli_error($config));

if(@mysqli_num_rows($sql_1) <= '0'){
    echo "";
}else{
    while($r_sql_1 = mysqli_fetch_array($sql_1)){
        $id_cidade_php = $r_sql_1[0];
        $estado_php = $r_sql_1[1];
    }
}

echo $cidade_selecionada = $php_cidade . " - " . $estado_php;

?>

Only one way to get the city selected in the "resp_geolocalizacao.php" within the pop-up to generate a cookie is missing.

Is it possible to recover this variable?

  • Do not use the snippet for code other than HTML/CSS/JS, please. The editor has 3 areas, none indicating PHP. For this use only the code sample tool (Ctrl+K).

  • The archive resp_geolocalizacao.php correctly returns the name of the city? If yes, possibly it will be available on JS at data, in the function(data){} that you defined in $.post;

  • Yes return to the city correctly.

No answers

Browser other questions tagged

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