16.PHP_Ajax模拟服务器登录验证

来源:互联网 发布:编辑pdf用什么软件 编辑:程序博客网 时间:2024/06/08 08:56

Ajax模拟登陆验证

index.php

<script language="javascript">
    var http_request = false;

    function createRequest(url){
        http_request = false;
        if(window.XMLHttpRequest){                   //Mozilla、Safari等浏览器
            http_request = new XMLHttpRequest();
        }else if(window.ActiveXObject){              //IE
            try{
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            }catch(e){
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e){}
            }
        }
        if(!http_request){
            alert("不能创建XMLHTTP实例!");
            return false;
        }
        http_request.onreadystatechange alertContents//指定相应方法
        //发出HTTP请求
        http_request.open("GET" ,url ,true);
        http_request.send(null);
    }

    function  alertContents() {
        if (http_request.readyState == 4) {//处理服务器返回的信息
            if (http_request.status == 200) {
                alert(http_request.responseText);
            else {
                alert("Ajax验证页面发生错误");
            }
        }
    }
</script>

<script language="javascript">
    function checkName(){
        var username = form1.name.value;
        if(username == ""){
            window.alert("name is null!");
            return false;
        }else {
            createRequest('checkname.php?username=' + username + '&nocache=' new Date().getTime());
        }
    }
</script>

<form name="form1" method="post" action="">
    <select name="name">
        <option value="xiaoming">xiaoming</option>
        <option value="xiaoli">xiaoli</option>
        <option value="xiaowang">xiaowang</option>
    </select>
    <input type="submit" name="Submit" value="Ajax" onclick="checkName()">
</form>



 

CheckName.php

<?php
    //模拟服务器验证
    $username $_GET['username'];
    echo 'Ajax Check web Get Name: '.$username;
?>

 

0 0
原创粉丝点击