php處理ajax

來源:互聯網
上載者:User

標籤:xhr   min   發送   input   eth   change   app   dom   put   

首先安裝wamp,若安裝過mysql則終止進程防止衝突,可以訪問localhost說明成功。在www目錄下建立項目,使用localhost訪問。

php:

<?php    //3.擷取ajax傳過來的內容處理    header("content-Type:text/text;charset=utf-8");        $username=$_POST[‘name‘];    if($username==‘admin‘){        echo ‘{"inf":"該使用者名稱不合法","sta":"0"}‘;    }    else if($username==‘malin‘){        echo ‘{"inf":"該使用者名稱已被註冊","sta":"1"}‘;    }    else{        echo ‘{"inf":"該使用者名稱可以註冊","sta":"2"}‘;    }    ?>

 

<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>        <style type="text/css">            .green{                color: green;            }            .red{                color: red;            }        </style>        <script>            window.onload=function(){                            function id(id){                    return document.getElementById(id);                }                function ajax(){                    var oAjax=new XMLHttpRequest();                    return oAjax;                }                id(‘username‘).onkeyup=function(){                                //1 建立ajax引擎                    var xhr=ajax();                //3 php後端操作                    var url=‘/m18AjaxT/checkNameJson.php?name=‘+id(‘username‘).value;                    xhr.open(‘POST‘,url,true);                    xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");                    var data="name="+id(‘username‘).value;                //2 發送請求,get用null,因為通過url發送                    xhr.send(data);                    xhr.onreadystatechange=function(){                        if(xhr.readyState==4 && xhr.status==200){                //4 渲染dom                                    var information=JSON.parse(xhr.responseText).inf;                            var sta=JSON.parse(xhr.responseText).sta;                            id(‘inf‘).innerHTML=information;                            if(sta==0){                                id(‘inf‘).className="red";                            }else if(sta==1){                                id(‘inf‘).className="red";                            }else{                                id(‘inf‘).className="green";                            }                        }                    }                }                            }        </script>            </head>    <body>    <form action="" method="get">        username:<input type="text" id="username" />        <input type="button" name="btn" id="btn" value="驗證" />    </form>            <span id="inf">this is infomation</span>        </body></html>

 

php處理ajax

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.