gpt4 book ai didi

php - jquery ajax如何验证数据库中是否存在记录

转载 作者:行者123 更新时间:2023-12-01 03:54:22 25 4
gpt4 key购买 nike

使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com