0
How to make a bank consultation with ajax
in the beforeSend
?
Example:
var formData = new FormData(this);
$.ajax({
url: "../_requeridos/cadastraPlano.php",
type: 'POST',
data: formData,
beforeSend: function() {
},
success: function (retorno) {...
I wonder if there is already the register in the bank before registering.
Something like:
beforeSend: function() {
acessa consultaRegistro.php
// pesquisa aqui e se o registro já houver alerta o navegador!
//Nesse caso, não chega a entrar no success. Ou seja, não faz a inserção no banco.
},
How to do this?
See how the Insert file is:
<?php
require_once "../_controles/_conexao/Conexao.php";
require_once "../_controles/_util/PhpUtil.php";
require_once "../_controles/_util/Constantes.php";
require_once "../_controles/_models/Planos.php";
require_once "../_controles/_models/Fotos.php";
require_once "../_controles/_models/Upload.php";
require_once "../_controles/_daos/PlanosDao.php";
require_once "../_controles/_daos/FotosDao.php";
require_once "../_controles/_daos/UploadDao.php";
$connection = new Conexao();
$conexao = $connection->abreConexao();
$phpUtil = new PhpUtil();
$constantes = new Constantes($phpUtil);
$planosDao = new PlanosDao($conexao);
$fotosDao = new FotosDao($conexao);
$nomePlano = $_POST["nomePlano"];
$descricao = $_POST["descricao"];
$plano = new Planos(
$nomePlano,
$descricao);
$pesquisaPlano = $planosDao->pesquisaPlanos("WHERE nome = '".$nomePlano."'");
if($pesquisaPlano == NULL) {
$cadastraPlano = $planosDao->cadastrar($plano);
$ultimoId = $planosDao->ultimoIdCadastrado();
require_once "upload.php";
} else $cadastraPlano = 3;
echo $cadastraPlano
?>
Friend you will do this back-end check, when your request arrives in php you make a query and check if it already exists.
– Maycon F. Castro
you mean in the insertion file itself? That is: before insertion
– Carlos Rocha
That’s right, buddy, because even if you can do it in beforeSend() you’ll have to make a request to know if the record exists.
– Maycon F. Castro
Yeah, that’s exactly what I wanted to do, do this check on before and if the record exists, don’t get to submit the form
– Carlos Rocha
This way it would be 2 requests in the back-end, and the fewer requests the better, you can do everything in the scope of the same request.
– Maycon F. Castro
how would you look? Can you give an example? I edited the question and put the insertion file at the end
– Carlos Rocha