alert(message)返回一个空字符串

时间:2015-04-28 06:57:30

标签: javascript php jquery json login

由@Hanky解决웃Panky!

我正在实施登录功能。

javascript部分:

function login() {
    var n = $('#userl').val();
    var p = $('#passl').val();
    if ( n != "" && p != ""){
        $.post("functions.php",{userl: n, passl: p}).done(function(mesaj){
            if(mesaj  == "")
               alert("!!!!!!!!!!EMPTY STRING!!!!!");
            else
            alert(mesaj);
        });
    }
    else {
        alert("Enter name and/or passwort");
    }
};

来自function.php的函数看起来是:

function login($nume,$parola)
{
    $sql = "SELECT id FROM `Users` WHERE nume ='".$nume."' ANY password = '".md5($parola)."'";
    $q = mysql_query($sql);
    if(!$q)
        die(json_encode(array("mesaj" => "Invalid")));
    else{
        $x = mysql_fetch_array($q);
        if (empty($x))
            die(json_encode(array('mesaj' => 'The user does not exist')));
        else {
        //    $user = new User($x['id']);
            session_start();
            $_SESSION['user_id'] = $x['id'];
            $_SESSION['loggedin'] = "yes";
            die(json_encode(array("mesaj"=>"you were logged")));
        }

        }
    //this part may be useless
    die(json_encode(array("mesaj"=>"you were not logged")));
}


if (isset($_POST['nume']) AND isset($_POST['parola']))
         login($_POST['nume'], $_POST['parola']);

问题是它一直警告EMPTY STRING,我认为它甚至没有从php访问我的登录功能......所以消息以它的方式返回:空

1 个答案:

答案 0 :(得分:3)

  $.post("functions.php",{userl: n, passl: p})

将其与

进行比较
if (isset($_POST['nume']) AND isset($_POST['parola']))

发现你的错误:)

您正在发送user1pass1,而在PHP中,您正在检查显然不存在的numeparola < / p>