dqsxsmi3704 2015-07-05 17:32
浏览 32
已采纳

使用AJAX检查用户名可用性

I'm currently trying to develop a small website in PHP to test my skills. However I found myself stuck when I decided to implement a live username availability check for the sign up process, as my JavaScript knowledge is somewhat basic.

Right now when I put some name in the username field, I only get the loading image and nothing else. This is what I have done:

My login.php page stored in the /views folder:

<section>
    <div>
        <form action="?action=login" method="post">
            <input type="text" placeholder="username" name="username" id="username" onkeypress ="check()">
            <span id="usernameStatus"></span>
        </form>
    </div>
</section>

My js file stored in /views/js:

function check () {
    var status = document.getElementById('usernameStatus');
    var u = document.getElementById('username').value;
    if(u != ""){
        status.innerHTML = '<img src="views/img/wait.gif" alt="" height="20" />';
        var hr = new XMLHttpRequest();
        hr.open("POST", "../login.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                status.innerHTML = hr.responseText;
            }
        }

        var v = "checkName="+u;
        hr.send(v);

    }
}

And finally the loginController.php stored in /controllers:

    public function run() {
        if(!empty($_POST["checkName"])){
            $name= htmlentities($_POST["checkName"]);
            if(Db::getInstance()->username_ok($name)){
                echo ("<strong>" . $name . "</strong> unavailable");
            }
            else{
                echo ("<strong>" . $name . "</strong> available");
            }
        }
        require_once('views/login.php');
    }

Could someone explain me what have I done wrong? Thanks!

  • 写回答

2条回答 默认 最新

  • douchui7332 2015-07-05 19:25
    关注

    I think the problem is with filenames.

    From your question, I guess the file structure is

    root
    |-- controllers
    |   `-- loginController.php
    `-- views
        `-- js
            `-- script.js
    

    In your JavaScript code which is in the file views/js/scriptname.js,

    hr.open("POST", "../login.php", true);
    

    request is sent to the file login.php.

    But, as you have mentioned, the path to your server side php script is /controllers/loginController.php.

    According to me, the JavaScript code should be as following:

    hr.open("POST", "controllers/loginController.php", true);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?