Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果所选用户名已存在,则提醒用户_Javascript_Php_Mysql - Fatal编程技术网

Javascript 如果所选用户名已存在,则提醒用户

Javascript 如果所选用户名已存在,则提醒用户,javascript,php,mysql,Javascript,Php,Mysql,此代码位于由表单的onsubmit事件触发的JavaScript函数中 var username = document.register.username.value; var phpUsernameFree = <?php $username = "<script>document.write(username)</script>"; $resultTempUsers = mysql_query("SELECT * FROM tempUsers

此代码位于由表单的
onsubmit
事件触发的JavaScript函数中

var username = document.register.username.value;
var phpUsernameFree = <?php 
    $username = "<script>document.write(username)</script>";
    $resultTempUsers = mysql_query("SELECT * FROM tempUsers WHERE username = '$username' ") or die(mysql_error());
    if( mysql_num_rows($resultTempUsers) == 0 ){
        echo 1;
    };
?> ;

if( phpUsernameFree == 0){
    toggleNew('usernameAlreadyExists', 1);
    usernameCounter = 1;
}
my existance.php如下所示:

<?php

*include connection to database here*

$data = $_POST["data"];
$type = $_POST["type"];
$resultUsers = mysql_query("SELECT * FROM users WHERE username = '$data' ") or die(mysql_error());
if( mysql_num_rows($resultUsers) == 1 ){
    echo 1;
}
?>
$("#inputId").keyUp(function(){
   //This event, raised when the textbox value changed
   //inside this event, you can call ajax function and check user existance and if result is false you can disable the submit button
});
<script>
function Login(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Msg").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "Login.php?q=" + str, true);
        xmlhttp.send();
        }
}
</script>

最好的方法是使用ajax。你应该这样做:

<?php

*include connection to database here*

$data = $_POST["data"];
$type = $_POST["type"];
$resultUsers = mysql_query("SELECT * FROM users WHERE username = '$data' ") or die(mysql_error());
if( mysql_num_rows($resultUsers) == 1 ){
    echo 1;
}
?>
$("#inputId").keyUp(function(){
   //This event, raised when the textbox value changed
   //inside this event, you can call ajax function and check user existance and if result is false you can disable the submit button
});
<script>
function Login(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Msg").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "Login.php?q=" + str, true);
        xmlhttp.send();
        }
}
</script>

如果您不想使用Jquery,则需要AJAX来实现这一点。 大概是这样的:

<?php

*include connection to database here*

$data = $_POST["data"];
$type = $_POST["type"];
$resultUsers = mysql_query("SELECT * FROM users WHERE username = '$data' ") or die(mysql_error());
if( mysql_num_rows($resultUsers) == 1 ){
    echo 1;
}
?>
$("#inputId").keyUp(function(){
   //This event, raised when the textbox value changed
   //inside this event, you can call ajax function and check user existance and if result is false you can disable the submit button
});
<script>
function Login(str) {
    if (str.length == 0) { 
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("Msg").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "Login.php?q=" + str, true);
        xmlhttp.send();
        }
}
</script>

类似的,不起作用,但这是一个想法。

使用普通按钮代替提交按钮,根据响应值在ajax响应后提交表单

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
        <script>
        function checkUserExistence(){
            var username = document.register.username.value;
            var xmlhttp;
            if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest();} else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 ) {
                    if(xmlhttp.status == 200){
                        phpUsernameFree = xmlhttp.responseText;
                        if( phpUsernameFree == 0){
                            alert("username Already Exists");
                        } else {
                            alert("username available.");
                            register.submit();
                        }
                    } else if(xmlhttp.status == 400) {
                        alert("There was an error 400");
                    } else {
                        alert("something else other than 200 was returned");
                    }
                }
            }
            xmlhttp.open("GET", "service.php?username=" + username, true);
            xmlhttp.send();
        }
        </script>
    </head>
    <body>
        <form id="register" name="register" method="post">
            <input type="text" name="username" id="username" value="check" />
            <input type="button" id="save" name="save" value="Save" onclick="checkUserExistence();" />
        </form>
    </body>
</html>

<!-- service.php -->
<?php
$username = $_REQUEST["username"];
$resultTempUsers = mysql_query("SELECT * FROM tempUsers WHERE username = '$username' ") or die(mysql_error());
if( mysql_num_rows($resultTempUsers) == 0 ){
    echo 1;
};
?>

测验
函数checkUserExistence(){
var username=document.register.username.value;
var-xmlhttp;
if(window.XMLHttpRequest){xmlhttp=new-XMLHttpRequest();}else{xmlhttp=new-ActiveXObject(“Microsoft.xmlhttp”);}
xmlhttp.onreadystatechange=函数(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
phpUsernameFree=xmlhttp.responseText;
if(phpUsernameFree==0){
警报(“用户名已存在”);
}否则{
警报(“用户名可用”);
注册。提交();
}
}else if(xmlhttp.status==400){
警报(“出现错误400”);
}否则{
警报(“返回了200以外的内容”);
}
}
}
open(“GET”,“service.php?username=“+username,true”);
xmlhttp.send();
}

看看ajax;如果您试图让浏览器执行此逻辑,则不会发生。您需要一些与服务器接口的东西。如果您使用jqueryajax函数,它会更简单……您可以参考这个示例了解代码