0

我只是无法弄清楚这段代码有什么问题。

我只是向服务器发送用户名和密码,然后服务器发回响应。服务器写入数据库没有问题,但在客户端有时它不会到达if(xmlhttp.readyState==4 && xmlhttp.status==200). 并在它执行 line 之后alert('login5'),jquery 动画重置。我知道这是 php 问题,但我不知道为什么它有时有效但有时无效,感谢您的帮助。

<script type = "text/javascript">

function sendLogin(){

    var xmlhttp;
    var getString;
    var url = "login.php";

    var username=document.getElementById('name').value;
    var password=document.getElementById('pw').value;
    var url= url+ "?username="+username+"&password="+password;


    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }else{// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("get", url , true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){

            alert("reached inside");
            getString = xmlhttp.responseText;
            alert(getString);
        }
    }

    xmlhttp.send();
    alert('login5');
    //problem here, have to wait a while
}

</script>  

html代码:

        <form id="logInBoxes">
            <input type="text" placeholder="username" id='name' size="15px">
            <input type="password" placeholder="pw" id='pw' size="10px">
            <input type="submit" value="Log In" onclick='sendLogin()'>
        </form>

php代码:

<?php
$username= $_GET['username'];
$password= $_GET['password'];

$salt = mcrypt_create_iv(32, MCRYPT_RAND);
$password = crypt($password, $salt);

$salt = mysql_real_escape_string($salt);
$password = mysql_real_escape_string($password);
$sql = mysqli_connect('localhost','root','','housescale');
// Check connection
if (mysqli_connect_errno()){echo "Failed to connect to MySQL: " . mysqli_connect_error();
}


mysqli_query($sql, "INSERT INTO user (username, password, salt) 
                VALUE 
                ('$username', '$password','$salt')")   or    trigger_error(mysql_error()); 



mysqli_close($sql);

echo $username;


?>

编辑:如果我alert('login5')在一个循环中再做 9 次,它就会起作用。这个延迟修复究竟意味着什么?

4

1 回答 1

1

这是我最终使用的代码:

测试.html

<!DOCTYPE html>
<html>
<head>
    <script type = "text/javascript">

    function sendLogin() {

        var xmlhttp;
        var getString;
        var url = "login.php";

        var username = document.getElementById( 'name' ).value;
        var password = document.getElementById( 'pw' ).value;
        url = url + "?username=" + username + "&password=" + password; //Don't Need to Re-Declare url Variable

        if( window.XMLHttpRequest ) {         // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {                              // code for IE6, IE5
            xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
        }

        xmlhttp.open( "get", url , true );
        xmlhttp.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" );

        xmlhttp.onreadystatechange = function() {

            if( xmlhttp.readyState == 4 ) {

                if( xmlhttp.status == 200 ) {   //Separated readyState and Status

                    alert( "reached inside" );
                    getString = xmlhttp.responseText;
                    alert( getString );
                }
            }
        };  //Missed Semi-Colon Here

        xmlhttp.send();
        alert( 'login5' );
    }
</script>
</head>
<body>
    <form id="logInBoxes">
        <input type="text" placeholder="username" id='name' size="15px">
        <input type="password" placeholder="pw" id='pw' size="10px">
        <input type="submit" value="Log In" onclick='sendLogin()'>
    </form>
</body>
</html>

登录.php

<?php
    $username= $_GET['username'];
    $password= $_GET['password'];

    $salt = mcrypt_create_iv( 32, MCRYPT_RAND );
    $password = crypt( $password, $salt );

    $sql = mysqli_connect( 'localhost', 'root', '', 'housescale' );
    $salt = mysqli_real_escape_string( $sql, $salt );
    $password = mysqli_real_escape_string( $sql, $password );

    // Check connection
    if ( mysqli_connect_errno() ){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    mysqli_query( $sql, "INSERT INTO user ( username, password, salt ) VALUE ( '$username', '$password', '$salt' )" )
        or trigger_error(mysql_error());

    mysqli_close( $sql );

    echo $username;
?>

我在 Windows 7 上使用 Mozilla Firefox 23。我的堆栈是 Uniform Server 8.8.2 (PHP 5.4.14 / MySQL 5.5.30)。

为了快速起见,我将表中的所有字段都设置为 varchar(255)。它对我来说只有一个问题,有时盐/地穴在我的数据库中变成空的,但这可能是一个字符集问题,因为它能够很好地回显几种不同的组合。

于 2013-08-21T03:00:27.753 回答