Get array of a php after an ajax request

1

I'm having to make an ajax request, which returns more than one value, and once I click on that value, another ajax request would be executed.

In order to do this request, I send an ajax request to php to make a select, where in that select it can return an array.

Sending the request and capturing 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("");

        }

Get 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);
        }

Do 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 that, as I said, when I click on this in the field of the generated div, I would have to make another select to list cnpj.

I thought of returning an array of php, that is the problem, the responseText of generateXMLHttp, it returns as a string, I wanted it to return as an array, to make it easier to know which link was clicked on. >

From now on, thanks guys

    
asked by anonymous 29.06.2017 / 19:52

0 answers