doujupa7567 2015-08-31 18:23
浏览 63
已采纳

Javascript使用实时输出验证PHP上的用户名

I want to use Javascript to validate my input username if it is correct or not showing result on realtime. Here is index.html code:

<html>
<head>
<script>
function showHint(str){
    if(str.length == 0){
        document.getElementById("hint").innerHTML = "";
    }else{
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function(){
            if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                document.getElementById("hint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("POST", "demo3.php?input=" + str, true);
        xmlhttp.send();
    }
}
</script>
</head>
<body>

Type a username: <br>
<input id="hint" type="text" name="username" oninput="showHint(this.value)"><p id="hint"></p>

</body>
</html>

Here is the demo3.php code:

<html>
<head>

</head>
<body>
<?php
$mysqli = new mysqli("localhost","root","123456","mini");

$username = $mysqli->real_escape_string($_REQUEST['input']);
$sql = "SELECT username FROM `users` WHERE username='$username'";
    $result = $mysqli->query($sql);
    if($result->num_rows){
        echo "Valid username";
    }else{
        echo "Invalid username";
    }

?>

</body>
</html>

I use the oninput event example from w3cschools, and I am wondering why my result do not show what I expect? And if I assign $username with static variable, demo3.php result seems to be correct feedback, not from index.html.

Plus, I am wondering how to validate multiple forms, such as password and email within the same validation php file. Ex:

input1 -> check username ->output check result
input2-> check password ->output check result
input3-> check email->output check result

New to javascript.All the tutorial seems to provide only one demo, not multiple examples.

  • 写回答

2条回答 默认 最新

  • dongwuzun4630 2015-08-31 20:42
    关注

    Since your input is being placed in the URL, you will need to use the GET parameter other than POST (which does not use the URL):

    xmlhttp.open("GET", "demo3.php?input=" + str, true);
    

    Now it should be able to pickup your input for $_REQUEST['input'] or $_GET['input']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(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?