我有一个文本字段类型的文本,当我输入一个车牌号码时,如果它存在,它必须显示一个警报,但事件 keyup 不起作用。
JS
$('#mat').keyup(function(){
var matric = $('#mat').val();
$.ajax({type: "POST",
url: "compruebaVeh.php",
data: "matric="+matric,
success:function(data) {
alert(data);
if(data=='ok'){
alert('this car already exists');
}
}
});
});
PHP
<?php
$mat=$_POST['matric'];
ini_set('display_errors',1); error_reporting(E_ALL);
require('conecta.php');
$cSQL="select matricula from vehiculos;";
$stmt=$oConni->prepare($cSQL) or die($oConni->error);
//$stmt->bind_param('s',$_POST['matric']);
$stmt->execute();
$stmt->bind_result($matricula);
while ($stmt->fetch()) {
if($matricula==$mat){
echo 'ok';
}
}
$stmt->close();
?>
最佳答案
在 php 代码中,赋值 $_POST['matric']=$mat;
应该反转
$mat = $_POST['matric'];
根据数据库中的数据,您的脚本可能会多次回显 ok
,循环应该变成:
while ($stmt->fetch()) {
if($matricula==$mat){
echo 'ok';
break;
}
}
所以现在您可以在 js 回调中与“ok”进行比较。
关于php - keyup 不起作用,不显示一个警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15368049/