bank query with ajax on beforeSend

0

How to query the bank with ajax on beforeSend ?

Example:

    var formData = new FormData(this); 

    $.ajax({
        url: "../_requeridos/cadastraPlano.php",
        type: 'POST',
        data: formData,
        beforeSend: function() {               
        },
        success: function (retorno) {...

I would like to know if the register already exists 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

?>
    
asked by anonymous 31.05.2018 / 14:37

1 answer

1

You'll do more or less like this:

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
  //Não faz o insert
}
} else {
  //Faz o insert
}
$conn->close();
?>

I hope to have helped my friend.

    
31.05.2018 / 15:57