douwei2713 2010-11-18 13:28
浏览 122
已采纳

如何在php和ajax中创建一个注册页面,检查某个用户名是否已经存在而没有页面刷新?

I have a register.php file which creates new users for my site . But if a person uses a username which is already existing an error is generated only when he enters the whole form and submits . How to implement Ajax/Jquery so as to show it without him submitting the form ?

  • 写回答

4条回答 默认 最新

  • dtf579777 2010-11-18 13:36
    关注

    It's actually not too bad if you're familiar with JS/Ajax Basics.

    You'll basically need to call a javascript function from your register page..

    HTML on Register Form

    <!--This is the textbox with the value we are checking-->
    <!--onkeyup can be substituted by any other event you wish to use intead-->
    <input onkeyup="checkUsername(this.value);" name="username" id="username" />
    
    <!--This is where we'll display the response-->
    <div id="response"></div>
    

    That JS function will create an ajax object, and pass the username variable to the PHP page for processing, and will await a response...

    JavaScript

    function checkUsername(username){
    
        //Construct the url, passing the username to the PHP page
        var url= 'checkNameAvailability.php?username=' + encodeURIComponent(username);
    
        if (ajax.readyState == 4 || ajax.readyState == 0) {
            ajax.open("POST", url, true);
            ajax.onreadystatechange = function (){
                if (ajax.readyState == 4) {    
    
                    //When you get the result from the PHP, put it in the response div
                    document.getElementById('response').innerHTML=ajax.responseText;
                }
            }; 
            ajax.send(null);
        }
    }
    
    //Just copy and paste this function - don't change it at all.
    function getXmlObject() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                showError('Status: Cound not create XmlHttpRequest Object. Consider upgrading your browser.','Please Wait');
            }
        }
    

    Then the PHP page, grabs the username variable, processes it in whatever way it needs to (is it available, is it long enough, does it have invalid chars, is it inappropriate, etc..) and returns a response.

    checkNameAvailability.php

    <?php
    
        //Accept a variable called 'username' that we are checking.
        $username=$_REQUEST['username'];
    
        //Run Checks to see if username is valid
        if ($username=="Dutchie") 
                die ("Username is reserved or already taken");
        if (strlen($username)<5) 
                die("The username is too short.");
    
       die("Username is Valid");
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图