Ihavetogetavariabletypearray
ofajax
anditisreturningcorrectlyuntilconsole.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);
?>