I need to type a text in an input on the form, it validates there even if this record already exists, that shows the result to the user and that does not allow to send the form without changing what is repeated.
form and js:
<form role="form" method="POST" action="php/cadastro.php" accept-charset="utf-8">
<div class="form-group">
<label for="exampleInputPassword1">Descrição</label>
<input type="text" class="form-control" name="descricao" id="descricao" required="required">
</div>
</form>
<script type="text/javascript">
$(function(){
$("input[name='descricao']").on('exit', function(){
var descricao = $(this).val();
$.get('arquivo.php?descricao=' + descricao, function(data){
$('#resultado').html(data);
});
});
});
</script>
file.php
<?php
$conexao = mysqli_connect('', '', '', '');
$descricao = filter_input(INPUT_GET, 'descricao');
$sql = "SELECT * FROM tabela WHERE descricao = '{$descricao}'";
$query = $mysqli->query( $sql );
if( $query->num_rows > 0 ) {
echo 'Já existe!';
} else {
echo 'Não existe ainda!';
}
?>
I would like it not to be a verification file, to verify with php in the form itself and to show the user already that he can not use such a description, without having to go to another file to search the registry and return to the page.
file upload to bank:
$conexao = mysqli_connect('', '', '', '');
$descricao = $_POST['descricao'];
$sql = "INSERT INTO tabela(descricao) VALUES ('$descricao')";
If anyone has suggestions, thank you.