Use array type variable that returns from ajax function in PHP?

-1

Ihavetogetavariabletypearrayofajaxanditisreturningcorrectlyuntilconsole.log(data);butI'mnotsurehowtousethereturnvariableinsidetheindex.phpfile.

Index.php

</!DOCTYPEhtml><html><head><linkrel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/style.css"> 
</head>
<body>
    <div>
    <?php
        $data='';
    ?>
    <label class="label">CNPJ</label>
    <input type="text" class="form-control" name="cnpj" value=""/>
    <button 
        type="button" 
        class="btn btn-success" 
        id="myBuscaCNPJ">Pesquisar
    </button>
    <?php
        var_dump($data);
    ?>
    </div>
    <script src="js/jquery-3.3.1.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="text/javascript">
        $("#myBuscaCNPJ").on('click', function(){
            $.ajax({
                type: "POST",
                url: "banco.php",
                dataType: 'json',
                data: { cnpj: $('input[name="cnpj"]').val() },
                success: function(data) { 
                    console.log(data)
                }
            })
        })                    
    </script>
</body>
</html>  

javascript

<script src="js/jquery-3.3.1.js"></script>
<script src="js/bootstrap.js"></script>
<script type="text/javascript">
    $("#myBuscaCNPJ").on('click', function(){
        $.ajax({
            type: "POST",
            url: "banco.php",
            dataType: 'json',
            data: { cnpj: $('input[name="cnpj"]').val() },
            success: function(data) { result(data) };
        }
    }                    
});
});
</script>

banco.php

<?php 
    $chave = $_POST['cnpj'];
    $db = mysqli_connect('localhost', 'root', '123456789', 'exemplo');
    $strQuery = 'select * from filiais where id = ' .$chave;
    $dados = mysqli_query($db, $strQuery);
    $dados_array = mysqli_fetch_array($dados);
    var_dump($dados_array);
    echo json_encode($dados_array);
?>
    
asked by anonymous 23.07.2018 / 17:11

2 answers

1

If you are doing POST at least you will need to get the variable

 $cnpj =htmlspecialchars($_POST["cnpj"]);
    
23.07.2018 / 19:02
1

Is the expected output likely to be a JSON?

 <?php 
 echo json_encode($dados_array);
 ?>
    
23.07.2018 / 21:17