允许登录表单在同一页面上处理php脚本而不刷新页面

时间:2013-03-28 03:59:01

标签: php html

修改
谢谢,Wes C我现在有了这个AJAX代码:

   <script type="text/javascript"> 
$(document).ready(function(){
    $("form[name^='login']").submit(function(event) {
        event.preventDefault();

        var dataToSend = 'username='+$("input[name^='username']").val()+'&password='+$("input[name^='password']").val();

        $.ajax({
            type: "POST",
            url: "index.php",
            data: dataToSend,
            success: function(response){
                if(response == "REDIRECT")
                {
                    window.location = "business_profiles/myReviews.php";
                else
                {
                    $("#my_error_div").html(response);
                }
            }
        });
    });
});
</script>

但是现在不是在您输入密码框时显示错误消息。我收到的只是一个警告框,上面写着“成功”,无论表格输入是什么。

好的,我已经尝试过,并试图自己解决这个问题。除了与其他编码人员联系,当然也搜索了stackoverflow的答案。没有人真的像我的情况。

我有一个简单的登录表单:

<form name="login" action="index.php" method="post">

        <ul>

            <li>
                <input type="text" name="username" autocomplete="off" placeholder="Username" autofocus="true"></li>

            <li>
                <input type="password" name="password" autocomplete="off" placeholder="Password"></li>
            <li>

                <input type="submit" name="submit" value="Login"> <a href="register.php" id="button">Register</a> <a href="forgot.php" id="button">Forgot Password</a> <a href = "javascript:void(0)" onclick = "document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'" id="button">Close</a>

            </li>
        </ul>
    </form>

此表单提交到它所在的同一页面。 (php脚本位于登录表单的正上方。),如下所示:

    <?php

//If the user has submitted the form
if($_POST['username']){
    //protect the posted value then store them to variables
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);

    //Check if the username or password boxes were not filled in
    if(!$username || !$password){
        //if not display an error message
        echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    }else{
        //if the were continue checking

        //select all rows from the table where the username matches the one entered by the user
        $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
        $num = mysql_num_rows($res);

        //check if there was not a match
        if($num == 0){
            //if not display an error message
            echo "<center>The <b>Username</b> you supplied does not exist!</center>";
        }else{
            //if there was a match continue checking

            //select all rows where the username and password match the ones submitted by the user
            $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
            $num = mysql_num_rows($res);

            //check if there was not a match
            if($num == 0){
                //if not display error message
                echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
            }else{
                //if there was continue checking

                //split all fields fom the correct row into an associative array
                $row = mysql_fetch_assoc($res);

                //check to see if the user has not activated their account yet
                if($row['active'] != 1){
                    //if not display error message
                    echo "<center>You have not yet <b>Activated</b> your account!</center>";
                }else{
                    //if they have log them in

                    //set the login session storing there id - we use this to see if they are logged in or not
                    $_SESSION['uid'] = $row['id'];
                    //show message
                    echo "<center>You have successfully logged in!</center>";

                    //update the online field to 50 seconds into the future
                    $time = date('U')+50;
                    mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

                    //redirect them to the usersonline page
                    echo 'REDIRECT';

                    exit;
                }
            }
        }
    }
}

?>

我需要表单来处理脚本而不刷新页面。我的登录表单位于一个灯箱内,因此当页面刷新灯箱时会再次出现错误,如密码无效,您必须再次单击“登录”以查找错误。我只是希望表单处理php脚本而不刷新页面,因此灯箱永远不会隐藏,直到用户成功登录。然后将用户重定向到他们的个人资料。

2 个答案:

答案 0 :(得分:1)

使用jQuery,您可以使用以下AJAX函数将登录表单中的数据发布到同一页面:

<script type="text/javascript"> 
$(document).ready(function(){
    $("form[name^='login']").submit(function(event) {
        event.preventDefault();

        var dataToSend = 'username='+$("input[name^='username']").val()+'&password='+$("input[name^='password']").val();

        $.ajax({
            type: "POST",
            url: "index.php",
            data: dataToSend,
            success: function(response){
                if(response == "REDIRECT")
                {
                    window.location = "business_profiles/myReviews.php";
                }
                else
                {
                    alert("Error: "+response);
                    $("#my_error_div").html(response);
                }
            }
        });
    });
});
</script>

另外,将PHP更改为:

<?php

//If the user has submitted the form
if(isset($_REQUEST['username'])){
    //protect the posted value then store them to variables
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);

    //Check if the username or password boxes were not filled in
    if(!$username || !$password){
        //if not display an error message
        echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    }else{
        //if the were continue checking

        //select all rows from the table where the username matches the one entered by the user
        $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
        $num = mysql_num_rows($res);

        //check if there was not a match
        if($num == 0){
            //if not display an error message
            echo "<center>The <b>Username</b> you supplied does not exist!</center>";
        }else{
            //if there was a match continue checking

            //select all rows where the username and password match the ones submitted by the user
            $res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."' AND `password` = '".$password."'");
            $num = mysql_num_rows($res);

            //check if there was not a match
            if($num == 0){
                //if not display error message
                echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
            }else{
                //if there was continue checking

                //split all fields fom the correct row into an associative array
                $row = mysql_fetch_assoc($res);

                //check to see if the user has not activated their account yet
                if($row['active'] != 1){
                    //if not display error message
                    echo "<center>You have not yet <b>Activated</b> your account!</center>";
                }else{
                    //if they have log them in

                    //set the login session storing there id - we use this to see if they are logged in or not
                    $_SESSION['uid'] = $row['id'];


                    //update the online field to 50 seconds into the future
                    $time = date('U')+50;
                    mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

                    //redirect them to the usersonline page
                    echo 'REDIRECT';
                }
            }
        }
    }

    exit;
}

?>

答案 1 :(得分:0)

第1步:在登录表单中添加div。

步骤2:验证用户控件,然后在ajax调用中加载页面

第3步:在ajax成功事件中,使用.html()附加要显示的页面