Maps Javascript Api - Problem saving searched location in Mysql database

Asked

Viewed 139 times

1

I need that when the user clicks the save button that appears when Mark receives a click, the location information is saved to the database.

Hidden fields that receive location values when searched:

            <input type="hidden" name="lat" id="lat">

            <input type="hidden" name="lng" id="lng">

            <input type="hidden" name="rua" id="rua">

            <input type="hidden" name="bairro" id="bairro">

            <input type="hidden" name="cidade" id="cidade">

            <input type="hidden" name="cep" id="cep">

Codes to (theoretically) pass the information to the file that makes the connection:

 // Define o que sera extraido da busca 
       var address = ''; //Endereço recebe nulo para posteriormente receber parametros em forma de array
       if (place.address_components) { // se o local tiver informações extras(componentes como cep, rua, bairro etc)
         address = [ // atribuindo os valores desejados a variavel address
           (place.address_components[0] && place.address_components[0].short_name || ''), // Numero do local
           (place.address_components[1] && place.address_components[1].short_name || ''), //Logradrouro, vulgo: nome da rua
           (place.address_components[2] && place.address_components[2].short_name || ''), // bairro
           (place.address_components[3] && place.address_components[3].short_name || ''), //cidade
           (place.address_components[6] && place.address_components[6].short_name || '') //cep

         ].join(' ');
       }

      var item_Lat = place.geometry.location.lat();
      var item_Lng = place.geometry.location.lng();
      var item_Numero = place.address_components[0];          
      var item_Rua = place.address_components[1];
      var item_Bairro =place.address_components[2];
      var item_Cidade =place.address_components[3];
      var item_Cep = place.address_components[6];

      $('#name').val(place.name);
      $('#lat').val(item_Lat);
      $('#lng').val(item_Lng);
      $('#rua').val(item_Rua);
      $('#bairro').val(item_Bairro);
      $('#cidade').val(item_Cidade);
      $('#cep').val(item_Cep);

      // Exibe na infoWindow o Endereço e informações do local
      infoWindow.setContent('<div><strong>' + name + '</strong><br>' + address + '<br><form method="POST" action="processa_cad.php"><input type="hidden" name="lat" id="lat"><input type="hidden" name="lng" id="lng"><input type="hidden" name="rua" id="rua"><input type="hidden" name="bairro" id="bairro"><input type="hidden" name="cidade" id="cidade"><input type="hidden" name="cep" id="cep"><input type="hidden" name="name" value=""><input type="hidden" name="type" value=""><input id="btnCadastrar" class="form-control btn btn-primary" type="submit" value="Salvar"></form>');

        marker.addListener('click', function(){
        infoWindow.open(map, marker);
        map.setCenter(marker.getPosition()); // centraliza o mapa no marcador atual, iniciado anteriormente
      });

File processes Cad.php:

<?php
session_start();
// ob_start();
require("conexao.php");

//Receber os dados do formulário
$cep = $_POST['cep'];
$rua = $_POST['rua'];
$cidade = $_POST['cidade'];
$bairro = $_POST['bairro'];
$lat = $_POST['lat'];
$lng = $_POST['lng'];
$type = $_POST['lng'];
$name = $_POST['name'];


//Salvar os dados no bd
$result_markers = "INSERT INTO enderecos(id_in_endereco,   cep_in_enderecos, logradouro_in_enderecos, cidade_var_enderecos, bairro_var_enderecos, lat_in_enderecos,  long_in_enderecos, nome_st_local, tipo_st_local ) VALUES 
            (2,  '$cep', '$rua' , '$cidade', '$bairro', '$lat','$name','$type')";

$resultado_markers = mysqli_query($conn, $result_markers);
if(mysqli_insert_id($conn)){
$_SESSION['msg'] = "<script  type='text/javascript'>alert('Local salvo');window.location.href='mapa.php'</script>";
header("Location: mapa.php");
}else{
$_SESSION['msg'] = "<script  type='text/javascript'>alert('Local não salvo');window.location.href='mapa.php'</script>";
header("Location: mapa.php");   
}

When I search for a location, it returns it to me in the center of the map with a marker under the location with some information as shown above. The problem is when I click save button, it just reloads the page processing the request, but always error when registering.

The only values that appear correct in the fields of type Hidden are only of Latitude and Longitude. The others return [object Object]

My fields in the bank are as follows:

Atributos do banco

  • Put the error in the question. Just one observation, if you are registering the latitude and longitude, I see no need to register the other data, you can search for this data by the maps api when necessary

  • So it doesn’t actually return me a specific error. It just doesn’t register in the bank as it should. And I need to register the other data for control in the users register

  • Variables have the correct value? you can use var_dump($POST) php (don’t forget to take the redirect to see the value)

  • The only values that appear correct in the fields of type Hidden are only of Latitude and Longitude. The others return [Object Object]

  • So the error when entering in the database, make sure you are getting the correct data from the google maps api

  • I will review how I am getting the information, but if you know any way to do this, please tell me kk but thank you anyway

  • If you want help with this, ask the question how you’re getting the data and a link with json example returned

  • Thank you very much for your attention William, I managed to solve the problem. I just forgot to post here!

Show 3 more comments
No answers

Browser other questions tagged

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