[英]How to check if username exists without refreshing page using wordpress
我想检查表单中的文本字段是否数据库中不存在用户名。我希望它不刷新页面并且我正在使用Wordpress。我知道可以通过Ajax实现,但我已经在Wordpress中尝试过Ajax,并且任何Ajax代码都没有不要在上面跑 请提供任何代码或任何有用的链接。 上次我尝试过此方法但没有起作用:
<?php
if(!empty($user_name)){
$usernamecheck = $wpdb->get_results("select id from wp_teacher_info where user_name='$user_name'");
if(!empty($usernamecheck)){
echo'username not available';
}
else {
}
}?>
<label for="user_name" id="user_name">Username: </label>
<input type="text" name="user_name" id="user_name" required/>
<span id="user-result" ></span>
<script type="text/javascript">
jQuery("#user_name").keyup(function (e) { //user types username on inputfiled
var user_name = jQuery(this).val(); //get the string typed by user
jQuery.post('teacher_form.php', {'user_name':user_name}, function(data) {
jQuery("#user-result").html(data); //dump the data received from PHP page
});
});
</script>
采用
if(!empty($_POST['user_name']){
$user_name = $_POST['user_name'];
成为
<?php
if(!empty($_POST['user_name']){
$user_name = $_POST['user_name'];
$usernamecheck = $wpdb->get_results("select id from wp_teacher_info where user_name='$user_name'");
if(!empty($usernamecheck)){
echo'username not available';
}
else {
}
}
?>
但是keyup事件会在每次keyup时调用ajax ..您可以使用**.blur()**
而不是**.keyup()**
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.