douyi9447 2012-10-05 03:09
浏览 16
已采纳

用户名检查页面

I'm creating a system that checks (via AJAX) if a given username is already taken. If so, then it alerts the user accordingly. I'm basing my system off of this tutorial and have tried adapting it to my site.

$(document).ready(function()//When the dom is ready
{
  $("#username").change(function()
  { //if theres a change in the username textbox

    var username = $("#username").val();//Get the value in the username textbox
    if(username.length > 3)//if the lenght greater than 3 characters
    {
      $("#availability_status").html('<img src="loader.gif" align="absmiddle">&nbsp;Checking availability...');
      //Add a loading image in the span id="availability_status"

      $.ajax({  //Make the Ajax Request
        type: "POST",
        url: "ajax_check_username.php",  //file name
        data: "username="+ username,  //data
        success: function(server_response){

          $("#availability_status").ajaxComplete(function(event, request){

            if(server_response == '0')//if ajax_check_username.php return value "0"
            {
              $("#availability_status").html('<img src="available.png" align="absmiddle"> <font color="Green"> Available </font>  ');
              //add this image to the span with id "availability_status"
            }
            else  if(server_response == '1')//if it returns "1"
            {
              $("#availability_status").html('<img src="not_available.png" align="absmiddle"> <font color="red">Not Available </font>');
            }

          });
        }

      });

    }
    else
    {

      $("#availability_status").html('<font color="#cc0000">Username too short</font>');
      //if in case the username is less than or equal 3 characters only
    }
    return false;
  });
});

ajax_check_username.php - HERE's where I think I'm having problems!!

<?php
include ("inc/db/db.php");

if($stmt = $mysqli->prepare('SELECT NULL FROM `bruger` WHERE `brugernavn` = ?'))
{
  $stmt->bind_param('s', $brugernavn);
  $brugernavn = $_POST["brugernavn"];
  $stmt->execute();
  $stmt->store_result();
  $count = $stmt->num_rows;
  $stmt->close();
  if($count = 0)
  {
    echo '1';
  }
  else
  {
    echo '2';
  }
}
?>


<tr>
  <td><p>Brugernavn</p></td>
  <td><input type="text" name="brugernavn" id="username"></td>
  <td><span id="availability_status"></span></td>
</tr>

ATTENTION I think I've made mistakes in my php, so it may be why it does not bother to do what I want it .. but it's just when I think.

This is what my html looking for when I need to check user name in the database. this is how I get no errors at all. it must be said that I never play with ajax or javascript before. so it will be a great help if you could help me further.

Feel free to ask if there is more you want to know something.

  • 写回答

2条回答 默认 最新

  • doulu0266 2012-10-05 03:16
    关注

    This if($count = 0) should be if($count == 0) if that affects your logic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)