1
I’m having to make an ajax request, which returns more than one value and as soon as I click on that value, another ajax request would be executed.
It turns out that to make this request, I send to php an ajax request to make a select, where in this select can return an array.
Send the requisition and capture the return
$("#txtRazaoSocial").keyup(function(){
var dados = $("#txtRazaoSocial").val();
if(dados.length > 2){
var XMLHttp = generateXMLHttp();
XMLHttp.open("get", "classes/getData.php?dados=" + dados, true);
XMLHttp.onreadystatechange = function () {
if (XMLHttp.readyState == 4)
if (XMLHttp.status == 200) {
var data = XMLHttp.responseText.split(/(\d+)(?=(?:\d{2}\/\d{2}\/\d{4}\sa))/);
if(data == ""){
$(".conteudo-select").html("");
$(".msgDados").html("Não foi encontrado nenhuma razao social");
}else{
$(".msgDados").html("");
$(".conteudo-select").fadeIn(500);
console.log(dados);
}
//var obj = {};
} else {
result.innerHTML = "Um erro ocorreu: " + XMLHttp.statusText;
}
};
XMLHttp.send(null);
}
else if(dados.length <= 2)
{
$(".conteudo-select").fadeOut(1000);
$(".conteudo-select").html("");
$(".msgDados").html("");
}
Take the data and call select
$dados = $_GET['dados'];
if($dados != ''){
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){
$Treinamento->getDados($dados);
}
else if(empty($dados)){
$Treinamento->getDados($dados);
}
else{
$Treinamento->getDados($dados);
}
Select
public function getDados($dados){
try{
$sql = "SELECT razao_social FROM prt_partner WHERE _ativo = 1 AND razao_social LIKE '%".$dados."%' ";
$stmt = DB::prepare($sql);
$stmt->execute();
foreach($stmt->fetchAll() as $key => $values){
$this->return = $values->razao_social;
print_r("<a id='".str_replace(" ","",$values->razao_social)."' '>".$this->return."<br>");
}
}catch (PDOException $ex){
$ex->getMessage();
}
}
The problem is, as I said, when I click on this in the generated div field, I would have to do another select to list cnpj.
I thought of returning a php array, here’s the problem, the generateXMLHttp responseText, it returns as a string, wanted it to return as an array, to make it easier for me to know which generated link was clicked on.
Thanks a lot guys
recommend you focus on the PHP side more or less like this: info##info2#info3##info4 and give data.split('###') to handle the javascript side array
– Bruno Folle
Thanks, it really helped your response
– gabrielfalieri