Home php教程 php手册 AJAX实现用户名验证

AJAX实现用户名验证

Jun 13, 2016 am 10:54 AM
ajax head html php for exist client document verify

[php]  

//该文件为register.php,在客户端  

 

 

用户注册  

 

    //创建ajax引擎  

    function getXmlHttpObject(){  

        var xmlHttpRequest;  

        //不同浏览器获取对象XmlHttpRequest对象方法不同  

        if(window.ActiveXObject){  

            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");  

        }else{  

            xmlHttpRequest = new XMLHttpRequest();  

        }  

        return xmlHttpRequest;  

    }  

    /* 

    function getXmlHttpObject(){ 

        //不同浏览器获取对象XmlHttpRequest对象方法不同 

        var xmlHttp = null; 

        try{ 

            //Firefox, Opera 8.0+, Safari 

            xmlHttp = new XMLHttpRequest(); 

        }catch(e){ 

            //Internet Explorer 

            try{ 

                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 

            }catch(e){ 

                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 

            } 

        } 

        return xmlHttp; 

    } 

    */  

      

    var myXmlHttpRequest = "";  

    //验证用户名是否存在  

    function checkName(){  

        //1号线  

        myXmlHttpRequest = getXmlHttpObject();  

  

        //判断创建成功?  

        if(myXmlHttpRequest){  

            //通过myXmlHttpRequest对象发送请求到服务器的某个页面  

            //第一个参数表示请求的方式, "get"/"post"  

            //第二个参数指定url,对哪个页面发出ajax请求(本质仍然是HTTP请求)  

            //第三个参数.true表示使用异步机制,false表示不使用异步机制  

              

            //注意:此处如果"username"和"="之间有空格,否则会出错,在服务器端接收不到"username";  

            //注意:如果"="之后有空格则将此空格也作为接收到的username的值的一部分。  

            //即在服务器端收到的username的值=“ ”(即空格)+从客户端发送的username值。  

            var url = "/AjaxTest/registerProcess.php?username=" + $("username1id").value;  

            //window.alert(url);  

              

            //打开请求  

            myXmlHttpRequest.open("get",url,true);  

            //指定回调函数.process是个函数名  

            myXmlHttpRequest.onreadystatechange = process;  

  

            //真正发送请求。如果是get请求则填入null即可  

            //如果是post请求,则填入实际的数据  

            //2号线  

            myXmlHttpRequest.send(null);              

              

        }  

    }  

  

    //回调函数  

    function process(){  

        //window.alert("这是回调函数" + myXmlHttpRequest.readyState);  

        //我要取出从registerProcess.php页面返回的数据  

          

        if(myXmlHttpRequest.readyState == 4){  

              

            //取出值,根据返回信息的格式而定  

            //window.alert("服务器返回" + myXmlHttpRequest.responseText);  

            //4号线  

            $('myResponse').value = myXmlHttpRequest.responseText;  

        }  

          

    }  

      

    function $(id){  

        return document.getElementById(id);  

    }  

 

 

 

   

 

        用户名:  

             

             

           
 

        密码:
 

        电子邮件:
 

         

   

 

   

 

        用户名:  

              

           
 

        密码:
 

        电子邮件:
 

         

   

 

 

 

[php] 

//该文件为registerProcess.php,在服务器端  

    //接收数据  

    $username = $_GET["username"];  

      

    if($username=="shunping"){  

        echo "用户名不可用";  

    }else{  

        echo "用户名可用";  

    }  

    echo "用户名:".$username;//3号线  

?>  

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

React's Role in HTML: Enhancing User Experience React's Role in HTML: Enhancing User Experience Apr 09, 2025 am 12:11 AM

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles