result of ajax in php session

0

I am filtering a cpf / cnpj through imput / datalist and checking the result with ajax. I want to bring the result in 2 php sessions but I can not.

The page that contains the filter:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<script type="text/javascript" src="jquery.min.224.js"></script>
</head>
<body>

<form method="post" id="formcpf">
<input type="text" name="pesquisa" id="pesquisa" class="cpfcnpj" list="lista" onBlur="run_pesq(this)">
<datalist id="lista">
<option value="111.111.111-11">
<option value="222.222.222-22">
<option value="333.333.333-33">
<option value="444.444.444-44">
</datalist>
</form>
<br /><br />
<div id="erro"></div>
<br /><br />
<?php echo $_SESSION['cpfcnpj'];?>
<br />
<?php echo $_SESSION['nome'];?>

<script>
// validar cpf 
function run_pesq(sel_pesq) {       
    var text = $("#pesquisa").val();
        if (text != "") {
            $.ajax({
                type: "POST",
                url: "validar.php",
                data: { pesquisa: text}
            })

            .done(function(data) {
                $('#erro').html(data);
                $("#formcpf")[0].form.reset();
            });
        }
}    
</body>
</html>

The validation page (validar.php):

<?php
include("banco.php");

if(isset($_POST["pesquisa"])){
    $qry = pg_query($db,"select * from tabela where cpfcnpj = '".$_POST["pesquisa"]."'") or die(pg_last_error($db));
    $qryRow = pg_fetch_assoc($db);

    if(pg_num_rows($qry)>0){
        $_SESSION['cpfcnpj'] = $qryRow['cpfcnpj'];
        $_SESSION['nome'] = $qryRow['nome'];
    }else{
        echo 'Sem resultados!';
    }
}
?>
    
asked by anonymous 03.05.2018 / 00:04

1 answer

-1

Take this onBlur="run_pesq(this)" and replace the run_pesq() function with this:

$('#pesquisa').blur(function(){ var text = $(this).val(); if (text != '') { $.ajax({ type: 'POST', url: 'validar.php', data:{'pesquisa': text}, success:function(){ $('#erro').html(data); $('#formcpf')[0].form.reset(); } }); } });

    
03.05.2018 / 16:35