JSON return to PHP with Ajax

Asked

Viewed 420 times

0

I’m trying to pass a JSON (result of a Google Place API) with Ajax to PHP. I believe my problem is in PHP when manipulating the data, follows a part of the code:

AJAX:

//GRAVA TODO O RETORNO NO BANCO...                
            $.ajax({

                types: 'GET',
                dataType: "json",
                url: 'http://'+VARendereco+'/WB_CADASTRO_USUARIO3/www/busca.php',
                data: "dados="+Dados_json+"&acao=salva_toda_busca_web",
                success: function(data){
                //alert(data[0].status);
                //$('#sub-pagina').html(data);
                //alert(data);
                alert("Gravou_busca2");
                }
            });

PHP:

if($_GET["acao"] == "salva_toda_busca_web"){
    //$matriz = stripslashes($_GET["dados"]);
    $matriz = json_decode($_GET["dados"], true);
    $itens = $matriz['results'];

        //foreach ( $itens as $e ){
    $SQL = "INSERT INTO tb_busca_geral (conteudo_busca, id_usuario, qt_resultados, local, lat, lon)VALUES('".$itens['name']."', 10, 5, '', '', '')";
    $num = mysqli_query($serve, $SQL);
    //echo $itens->name; 
        //} 
}

It records the record, more blank in this field... If anyone has any ideas, I’d appreciate it!

json (example Google, in this format):

inicio

final

  • It may be a typo, you wrote there: $$_GET["data"] , and the correct is: $_GET["data"]

  • Thanks Viniam, but it was only time to copy and paste right here, I’ve reviewed, plus it saves the record in mysql... only blank!

  • gives a var_dump($_GET["data"] ) before writing to mysql to see where you’re losing that information

  • Do the following: delete the 2 final lines, and echo the $items and check that the data is being received correctly, if everything is correct, Try this: $SQL = 'INSERT INTO tb_busca_geral (conteudo_busca, id_usuario, qt_resultados)VALUES("'.$items['name']'.'", 10, 5)',,,REMEMBERING: if all fields are varchar put everything including the numbers in double quotes.

  • Viniam, I echo and return blank to AJAX...

  • Follow the json result as Gabriel asked... Remembering that the json result of the request is ok, because I am working with it in other functions:

  • Sorry I can’t paste the return in json in question kkk... All wrong formatting appears

  • I put two images referring to the json that returns from the url, I’m manipulating it normally with javascript, the problem is in php!

  • dados_json is a JSON String or an Object already ?

  • Command as an object yet, I try to convert with "json_decode" in php

Show 5 more comments

1 answer

0

I managed to solve it this way:

$matriz = json_decode ( json_encode ($_POST['dados']), true);

I do not understand exactly why it worked only this way, encoding and decoding again, if someone can explain put there... But it worked perfectly with a "foreach", and I took the opportunity to move to POST that has more transfer capacity resources and is considered more secure...

Thank you very much!!!

Browser other questions tagged

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