douhoujun9304 2015-10-23 14:32
浏览 88
已采纳

使用ajax检查文本输入字段的值

I have a website where the log ins are screen names. On the create user form I want to be able to have ajax check if a screen name exists already as it is typed into the form.

This is the HTML form input field

<label for="screenName">Screen Name:
<input type="text" class="form-control" name="screenName" id="screenName" size="28" required>
<div class="screenNameError"></div>

A message should be displayed in the <div class="screenNameError"></div>line if the username matches the database.

This is my Jquery code for this.

$(document).ready(function(){

if ($('#screenName').length > 0){

    var screenName = $("input").keyup(function(){
        var value = $(this).val();
        return value;
    })

    $.ajax({
        type: 'post',
        url:  'screenNameCheck.php',
        data: 'Screen_Name=' + screenName,

        success: function (r) {
            $('.screenNameError').html(r);
        }
    })

}

});

This is the PHP file that gets called to make the DB query

$screenName = $_POST['Screen_Name'];

$screenNameSQL = "SELECT Screen_Name FROM Users WHERE Screen_Name = '$screenName'";
$result = $my_dbhandle->query($screenNameSQL); //Query database
$numResults = $result->num_rows; //Count number of results

$resultCount = intval($numResults);

if($resultCount > 0){
    echo "The username entered already exists. Please a different user name.";
}

For some reason my Jquery is not firing when I type the username in the form :/

Thanks in advance

  • 写回答

4条回答 默认 最新

  • dto5637 2015-10-23 15:15
    关注

    Try changing your jQuery to this -

    $(document).ready(function() {
      $('#screenName').keyup(function() {
        var value = $(this).val();
        $.ajax({
          type: 'post',
          url: 'screenNameCheck.php',
          data: 'Screen_Name=' + value,
          success: function(r) {
            $('.screenNameError').html(r);
          }
        });
      });
    });
    

    However you probably want to minimise the number of ajax requests being made so I would advise putting your ajax request into a setTimeout functon and clearing it with each subsequent keypress. -

    $(document).ready(function() {
      var ajaxRequest;
      $('#screenName').keyup(function() {
        var value = $(this).val();
        clearTimeout(ajaxRequest);
        ajaxRequest = setTimeout(function(sn) {
          $.ajax({
            type: 'post',
            url: 'screenNameCheck.php',
            data: 'Screen_Name=' + value,
            success: function(r) {
              $('.screenNameError').html(r);
            }
          });
        }, 500, value);
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100