使用 jquery ajax 我有一个文本字段,其中输入了名称,当用户输入名称时,ajax 检查该名称是否已存在于数据库中,如果存在,则显示一条消息,表明该名称已存在并且表单在输入不存在的名称之前无法提交?
这将是 php
<?php
$name = $security->secure($_POST['name']) //security->secure() cleans the field
$query = $db->query("select name from table where name = '$name'");
if($db->num_rows($query) != 0) { "that record already exists"; }
//disable the form button until a name is entered that does not exist.
?>
最佳答案
在 Jquery 中,您可以使用 $.ajax、$.post 甚至 $.get 方法来进行简单的 ajax 调用 示例:
$(document).ready(function(){
$.ajax({
type: 'POST', /*the method u are using*/
url: url, /* http://yourdomain.com/post.php */
success: function(data){alert('Name exists')} /* What to do in case of success */
});
});
我希望这足以帮助您完成这项工作。
关于php - jquery ajax如何验证数据库中是否存在记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4382535/