php没有响应ajax请求

时间:2017-01-15 17:56:20

标签: php jquery html ajax callback

我是jquery和php的新手。我设计了一个html登录表单。点是我在各种编码技术上笨拙,以下代码无效。

var inputs = $('#loginid, #pass').serialize();
$(".btn_loginsubmit").click(function() {
    var loginid = $("#loginid").val().trim();
    var pass = $("#pass").val().trim();

    if( (loginid != "") && (password != "")) {
        $.ajax({
            url:        '/inc/login.php',
            dataType:   'json',
            data:       inputs,
            type:       'post',
            success:    function(data) {
                $(".popup_login").fadeOut('fast');
                $(".alert_login").fadeIn('fast');
                $("#message_login").empty();
                $("#message_login").html(data);

                clear();
            }
        });
    else {
        $(".popup_login").fadeOut('fast');
        $(".alert_login").fadeIn('fast');
        $("#message_login").empty();
        $("#message_login").html("Enter details.");
    }
}

php代码是:

<?php
    include_once('phpfunctions.php');

    $rval = "";

    if(isset($_POST['loginid']) && isset($_POST['pass'])) {

        $loginid = mysql_real_escape_string($_POST['loginid']);
        $pass = mysql_real_escape_string($_POST['pass']);

        $qrymailchk = "SELECT * from master_db where user_mail='$loginid' AND user_pass='$pass'";
        $qryphonechk = "SELECT * from master_db where user_phone='$loginid' AND user_pass='$pass'";

        $resmailchk = mysqli_query($conn, $qrymailchk);
        $resphonechk = mysqli_query($conn, $qryphonechk);

        $row1 = mysqli_fetch_array($resmailchk, MYSQLI_BOTH);
        $row2 = mysqli_fetch_array($resphonechk, MYSQLI_BOTH);

        if($row1 || $row2) {
            session_start();

            $_SESSION["uid"] = ($row1) ? $row1['user_code'] : $row2['user_code'];

            $_SESSION["un"] = ($row1) ? $row1['user_name'] : $row2['user_name'];
            $_SESSION["em"] = ($row1) ? $row1['user_mail'] : $row2['user_mail'];
            $_SESSION["ph"] = ($row1) ? $row1['user_phone'] : $row2['user_phone'];

            $words = explode(" ",$_SESSION["un"]);
            $_SESSION["fn"] = $words[0];
            ?>

            <script>alert("User logged in!");</script>

            <?php

            $rval = "Logged in as ".$_SESSION["fn"];
            echo $rval;

            $URL = "/services.php";
            echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';
                    echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";

            exit();
        }
        else{
            $rval = "Invalid LoginID Password combination!";
            echo $rval;
            mysqli_close($conn);
        }
    }   
?>

以下是输入的html代码:

<div class="inputfields_login">
    <a class="head_loginform">Login to Existing Account</a>
    <br>
    <br>
    <div class="input_id">
        <input type="text" name="loginid" id="loginid" placeholder="Login ID" <?php if (!empty($_POST['loginid'])) {echo "value=\"" . $_POST["loginid"] . "\"";} ?> >
    </div>
    <div class="input_pass">
        <input type="password" name="password" id="pass" placeholder="Password" >
    </div>
</div>

现在,我知道php代码正在运行,因为我使用$ .post()调用这个相同的代码,它按预期工作。登录成功后,页面被重定向到用户区域。

但是,在这种情况下它不起作用并且看起来没有看到呼叫。当我单击提交按钮时,页面deos不执行任何操作,并且重定向不起作用。即使控制台日志仍然是空的。

请注意,我需要一些关于此代码中出现问题的帮助以及有关如何使其工作的一些建议。

0 个答案:

没有答案