2
I am doing a section on my website, in which there is a field select
. When I select something in that select
, i want to conduct a BD search by returning the information about that item selected to use this information in other parts of the site.
I thought I’d do it using jquery and ajax. I can carry out the search, but I’m having trouble catching the return of the search.
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 it the right way?
Or is there a better way to do it?
Thank you.
Missing you report how this ajax will work, such as data in JSON, HTML or TEXT.
– Romario Pires
Does Alert appear? Does it give an error in the console? what
var_dump($s);
?– Sergio
There is another, echo did not print an array without you inform which index, you have to handle the data output.
– Romario Pires
var_dump prints the found array. Success Alert gives an array to string conversion error.
– Ricardo Afonso
Hi Romario, and what would be the right use?
– Ricardo Afonso
Romário, so you don’t go to the cup again, huh!
– Diego Souza
Okay, so do it
echo json_encode($r);
and in AJAX togetherdataType :'json',
in the configuration object. Works?– Sergio
Exactly the way Sergio did it is the best option to use!
– Romario Pires
Thanks Sergio, it worked with JSON. I didn’t know him. It will help a lot!
– Ricardo Afonso