未捕获的TypeError:string不是函数

时间:2014-06-10 12:09:49

标签: javascript php

我正在尝试创建一个登录部分,该部分应该使用电子邮件ID /电话号码和密码,并检查数据库。应该是这么简单,我得到一个TypeError,在行上声明“字符串不是函数” -

if ($e_check!=0) {

以下代码:

  <?php
if (isset($_POST['username']) && isset($_POST['password'])) {
    include_once("db_conx.php");
    $u = $_POST["username"];
    $p1 = $_POST["password"];
    $p = md5($p1);
    if ($u!=""){
        if (is_numeric($u)) {
            $sql = "SELECT * FROM users WHERE phone='$u' AND password='$p' LIMIT 1"; //check against phone numbers
            $query = mysqli_query($con, $sql);
             $result = mysqli_result($query);
        $e_check = mysqli_num_rows($result);


            //var_dump($e_check);
             if ($e_check != 0) {
                echo "Success!";
                exit();
            }
            else {
                echo "Password is incorrect";
                exit();
            } 
        }

        else {
            $sql = "SELECT * FROM users WHERE email='$u' AND password='$p' LIMIT 1"; //check against email
            $query = mysqli_query($con, $sql);
             $result = mysqli_result($query);
        $e_check = mysqli_num_rows($result);

            var_dump($e_check);
             if ($e_check != 0) {
                echo "Success!";
                exit();
            }
            else {
                echo "Password is incorrect";
                exit();
            } 

        }
    }
}

?>

<html>
<head>
<meta charset="UTF-8">
<title>Sign In</title>

<script src="ajax.js"></script>

<script type='text/javascript'>

function login(){
    var username = document.getElementById("username").value();
    var password = document.getElementById("pwd").value();
    var status = document.getElementById("status");
    if (username=="" || password == "") {
        status.innerHTML = "Email/Phone or Password is missing";
    }

    else {
        status.innerHTML = "Please wait...";
        var ajax = ajaxObj("POST", "index2.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText != "Success!"){ ///returned from php file
                    status.innerHTML = ajax.responseText;

                }
                else {
                    //alert("Logged in");
                    //do nothing
                }
            }
        }
       ajax.send("username="+username+"&password="+password); //shoots variable to php 

    }

}
</script>
</head>
<body>
<div id="pageRight">
    <form id="sign-up" onSubmit="return false;">
        Email/Phone: <input type="text" id="username" >
        Password: <input type="password" id="pwd">
        <button id="signin" onClick="login()" style="background-color:#9C092B; color: white; padding: 9px 15px">Sign In </button><br>
        <span id="status" style="color: red"></span>
    </form>
</div>
</body>
</html>

我理解变量e_check没有检索到正确的返回类型,但我不明白为什么会发生这种情况。有没有更好的方法来检查查询是否成功,或者您是否可以建议对现有代码进行任何修改以删除此错误?我是PHP的新手,如果这是一个愚蠢的问题,我道歉。在此先感谢您的帮助! :)

3 个答案:

答案 0 :(得分:5)

它&#39; S

var username = document.getElementById("username").value;
var password = document.getElementById("pwd").value;

var username = document.getElementById("username").value();
var password = document.getElementById("pwd").value();

答案 1 :(得分:0)

你需要在语句中使用空格(你在if语句的两个条件中都有这个空格),它只是不好的形式 -

if ($e_check != 0) {

然而,正如其他人所指出的那样,这不太可能是你的问题。 TypeError问题是JavaScript之一,而不是PHP。您创建的语句将在PHP中运行。

答案 2 :(得分:0)

您需要一个结果,以获得num_row计数。使用此:

        $sql = "SELECT * FROM users WHERE phone='$u' AND password='$p' LIMIT 1"; //check against phone numbers
        $query = mysqli_query($con, $sql);
        $result = mysqli_result($query);
        $e_check = mysqli_num_rows($result);