I'm making a section on my site, which has a select
field. When I select something in this select
, I want to perform a search on the DB, returning information about that selected item to use that information elsewhere in the site.
I thought about doing this using jquery and ajax . I can do the search, but I'm having trouble fetching the search return.
Ajax:
$('#sedes').on('change', function() {
var idLoja = this.value;
alert(idLoja);
$.ajax({
url: "carregarInfo.php",
type: "POST",
data: {
id: idLoja
},
cache: false,
processData:true,
success: function(data)
{
alert(data);
}
});
});
PHP:
<?php
require_once('conexao.php');
$idLoja = $_POST['id'];
$sql = "SELECT * FROM lojas WHERE idLoja = '$idLoja'";
$result = $conexao->query($sql) OR trigger_error($conexao->error."[$sql]");
$s = mysqli_fetch_array($result);
echo $s;
How can I do this the right way?
Or is there a better way to do this?
Thank you.