Check equal data in SQL with onBlur

Asked

Viewed 355 times

1

I need you to type in input, it check in the database if there is an equal record, as those user creation fields in email.

I believe it’s in the onBlur of input, and use a if == in the command, the problem is: how I call SQL values for comparison?

No search button, it has to be while typing (onblur).

The input is limited to 7 characters.

<input name="placa" id="placa" type="text" value="" pattern="[A-Za-z]{3}[0-9]{4}" size="20" maxlength="7" required onkeyup="this.value = this.value.toUpperCase();" onblur="verifExists(digs)"> 

DIV hidden after input

<div class="style12" id="jaexistedv" style="display:none">Placa j&aacute; registrada!</div>

PHP + Java

$search_buscar = "-1";
if (isset($_GET['placa'])) {
  $search_buscar = (get_magic_quotes_gpc()) ? $_GET['placa'] : addslashes($_GET['placa']);
}

mysql_select_db($database_LocalPHP, $LocalPHP);
$query_buscar = sprintf("SELECT * FROM bdsul WHERE bdsul.placa LIKE '%s'", $search_buscar);
$buscar = mysql_query($query_buscar, $LocalPHP) or die(mysql_error());
$row_buscar = mysql_fetch_assoc($buscar);
$totalRows_buscar = mysql_num_rows($buscar);

<script type="application/javascript">
        function verifExists(digs) {
        var regpl = <?php echo $row_buscar['placa']; ?>;
        if (digs == regpl) {
            document.getElementById("jaexistedv").style = "display:show";
            }
        }
    </script>
  • You will need an Ajax call to access the database. Which backend language you are using?

  • PHP+Javascript. I’m not using include/require Once except for the connection. CSS + Javascript all included in the document itself and not in the same place, so scattered in the middle of the document, for example: when there is an event in a select, the "application/javascript" ta before.

1 answer

1

Using Jquery and Ajax, you could do more or less like this:

    $("#registro").on("blur", function(){

       var valor = $(this).val();

       $.ajax({
          url: "url_da_pagina_backend", // página onde você acessará o banco para fazer a checagem
          data: {
              valor: valor // passa o valor para o backend para checagem
          },
          type: "POST",
          success: function (data) {
              if(data == 'existe'){ // apenas um exemplo de checagem
                  // informe o erro, deixe o campo vermelho, etc.
          }
        }
     });
  });

If you don’t use Jquery, with pure Javascript it would be like this:

 function checarRegistro() { // Função que voce vai chamar no onBlur do campo
    // Cria um objeto ajax para fazer a requisição
    var ajax = new XMLHttpRequest();
    // Pega o valor do campo
    var registro = document.getElementById("registro");
    // Seta tipo de requisição e URL com os parâmetros
    ajax.open("GET", "url_da_pagina_backend/?registro=" + registro, true);
    // Envia a requisição
    ajax.send();
    // Cria um evento para receber o retorno.
    ajax.onreadystatechange = function() {
    // Caso o state seja 4 e o http.status for 200, é porque a requisição deu certo.
   if (ajax.readyState == 4 && ajax.status == 200) {
     // Retorno da requisição
     var data = ajax.responseText;
     // Aqui você verifica o retorno e faz a checagem
     if (data == "existe") {
       // faz alguma coisa
     }
   }
 }
}
  • I have serious problems understanding this url , I tried searching for the syntax in w3schools but I don’t make it work at all. In the case of the page I made, there is no result target page, it gets on the same page.

  • Then put the code you already have in the question, so people analyze it better.

  • I put what I have (it has more than 950 lines, so I separated only the related input and communication with Mysql).

  • Right, so on this very page, in PHP code, you will have to create a function that receives via Post the value that the user received in the field (the code I have already put shows how to send the value). You then make a select in the database seeking this value, if it is found is because it already exists, then you return some value stating that already exists.

  • I was trying to do like the select + javascript, where I have 3 div in the way style="display:None" and only changes to display:show when selected. I was thinking of using the same idea, leave a div with display:None and just change the document.getElementById("xxx").style = "display:show"; when you return the message saying that this value already exists in the database. I was trying to turn the search into a variable to compare with the typed in input just got me mixed up in a lot of stuff along the way...

  • I changed more or less as I tried to use again. I have no way to use url, no new page, every result is on the same page.

Show 1 more comment

Browser other questions tagged

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