0
I have an event onblur to call a function and this function has to call a php class in order to compare what was typed in the input with what is in my BD, but I don’t know how to do it using jquery, how do this?
HTML
<html>
<head>
<title>Validação de campos com AJAX</title>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script type="text/javascript" src="assets/ajax/check_cnae.js"></script>
</head>
<body>
Login: <br /> <input type="text" id="cnae" onblur="validarDados('cnae', document.getElementById('cnae').value);" />
<div id="campo_cnae"> </div>
</body>
</html>
class
require_once "Conexao.class.php";
class Socio {
private $con;
private $cnae;
public function __construct(){
$this->con = new Conexao();
}
public function __set($atributo, $valor){
$this->$atributo = $valor;
}
public function __get($atributo){
return $this->$atributo;
}
public function cnae(){
$this->cnae = $_GET['valor'];
$validar = $this->con->conectar()->prepare("SELECT cnae FROM cnae WHERE cnae = ?");
$validar->execute(array($this->cnae));
if($validar->rowCount() == 0){
echo "CNAE invalido.";
}else{
echo "CNAE valido.";
}
}
script
var req;
function validarDados(campo, valor) {
// Verificar o Browser
// Firefox, Google Chrome, Safari e outros
if(window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
// Internet Explorer
else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
// Aqui vai o valor e o nome do campo que pediu a requisição.
var url = "../../index_teste2.php?campo="+campo+"&valor="+valor;
// Chamada do método open para processar a requisição
req.open("Get", url, true);
// Quando o objeto recebe o retorno, chamamos a seguinte função;
req.onreadystatechange = function() {
// Exibe a mensagem "Verificando" enquanto carrega
if(req.readyState == 1) {
document.getElementById('campo_' + campo + '').innerHTML = '<font color="gray">Verificando...</font>';
}
// Verifica se o Ajax realizou todas as operações corretamente (essencial)
if(req.readyState == 4 && req.status == 200) {
// Resposta retornada pelo validacao.php
var resposta = req.responseText;
// Abaixo colocamos a resposta na div do campo que fez a requisição
document.getElementById('campo_'+ campo +'').innerHTML = resposta;
}
}
req.send(null);
}
PHP file that receives instantiation
require_once 'classes/Socio.class.php';
$socio = new Socio();
if(isset($_GET['valor'])){
$dados = $socio->cnae($_GET['valor']);
}
You need to do, in function
validarDados()
, an Ajax call for some PHP file and this file should do the checking, then just return some result with PHP and handle in Javascript.– Gabriel Queiroz Schicora
It’s just this part that I don’t know @Gabrielqueirozschicora, I’m watching some videos on youtube to see if it helps.
– smourao
I put an example in the answers
– Gabriel Queiroz Schicora
@Gabrielqueirozschicora I even put a script in ajax, but I can’t access mine Function cnae, I’ll edit and put the script I tried.
– smourao
You’re sending a requisition to your class(
Socio.class.php
), but when it gets there, nothing happens, because nowhere is being called its functioncnae
, she’s just being raised, so you’d have to call her somewherecnae();
or, as would be the most correct in my opinion, create a separate file that will instantiate your class and call the cnae function, as is exemplified in my reply, after all send an ajax request to a class I think it is not a good practice.– Gabriel Queiroz Schicora
@Gabrielqueirozschicora, I managed to get the result I wanted, but there’s only one problem, the return is duplicating my input and I couldn’t find the error, [link]http://www.acivg.com/index_teste.php, I edited the code to see how it looked, I didn’t use your example but opened the mind.
– smourao
@Gabrielqueirozschicora, thanks for the tip, I managed to get the result and solved the problem of duplication of input, was instantiating in the same php file that received the answer, I had to separate and do in 4 files, now working the way I want.
– smourao
I’m glad you solved, we need to be there.
– Gabriel Queiroz Schicora