query the bank with ajax in beforeSend

Asked

Viewed 110 times

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.

  • you mean in the insertion file itself? That is: before insertion

  • 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.

  • 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

  • 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.

  • how would you look? Can you give an example? I edited the question and put the insertion file at the end

Show 1 more comment

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 I helped friend.

  • But this way, it’s the way I posted the question. That is, a new query (Insert) will be done the same way! But thanks for the guidance on this holiday!

  • If you did in beforeSend() he would have to make a request to know if it exists and with the return he would continue in the rest of the ajax scope and make a new request just to insert the data in the bank. From this model he checks only one request.

  • Ahh got it. A new ajax request and not SQL. Okay. Now it’s clear! Do you think you can help me with this other question? https://answall.com/questions/303364/plugin-creatingspace%C3%A7o-empty-below-do-html-when-resizes-a-canvas

  • I took a look at this other question, unfortunately I could not help you friend.

  • Okay, thank you anyway!

Browser other questions tagged

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