1
I need that when typing a text in an input form, it validates right there if there is already this record, that shows the result pro user and 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>
php file.
<?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 through a file to check, to check with php in the form itself and already show to the user already realize that can not use such description, without having to go to another file to fetch the record and back to the page.
bank sending file:
$conexao = mysqli_connect('', '', '', '');
$descricao = $_POST['descricao'];
$sql = "INSERT INTO tabela(descricao) VALUES ('$descricao')";
If anyone has any suggestions, I’d appreciate it.
You will have to check via ajax in the input Blur event, this is the most basic way, which your code?
– sNniffer
Ok, post the code you already have and what output it generates. If it is an error, post the error message as well.
– Woss
Okay, I’ll post what I have, but earlier I tried using ajax based on some searches, but it didn’t work.
– R.Gasparin