ajax result in php session

Asked

Viewed 51 times

0

I’m filtering a Cpf/cnpj through imput/datalist and checking the result with ajax. I intend to bring the result in 2 sessions php but I’m not getting it.

The page containing 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 (validate.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!';
    }
}
?>

1 answer

-1

Strip that onBlur="run_pesq(this)" and replaces the function run_pesq() therefore:

$('#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(); } }); } });

Browser other questions tagged

You are not signed in. Login or sign up in order to post.