当前位置: 首页 > 知识库问答 >
问题:

PHP和AJAX登录表单

施永宁
2023-03-14

您好,我知道这个主题以前就有人提出过,但是没有人像我这样做过AJAX操作标准。我是AJAX和jQuery的新手

我这里的问题是,我不确定js函数“ajaxPostCallManip()”是否达到“login.php”!!

我发了几个警报,但没有出现...请帮帮忙

超文本标记语言代码

    <link rel="stylesheet" type="text/css" href="jQuery-Files/jquery.mobile-1.3.1.css"/>
    <script type="text/javascript" src="jQuery-Files/jquery-2.0.2.js"></script>
    <script type="text/javascript" src="jQuery-Files/jquery.mobile-1.3.1.js"></script>
    <script type="text/javascript" src="js/ajax.js"></script>

<div data-role="page" id="loginDialog">
    <header data-role="header">
        <h3 style="margin-left: auto; margin-right: auto;">Login or <a href="#regpage"      
            data-transition="flip">Register</a></h3>
    </header>
    <div data-role="content">
        <form method="POST" action="" onsubmit="check_login(); return false;">
             <fieldset data-role="contolgroup">                            
                  <div data-role="fieldcontain">                    
                       <label for="login_username">Username</label>
                       <input type="text" name="login_username" id="login_username" 
                              placeholder="username" />

                       <label for="login_pwd">Password</label>
                       <input type="password" name="login_pwd" id="login_pwd" 
                              placeholder="password" />

                       <div style="margin: auto; text-align: center;">
                           <label for="login_submit" class="ui-hidden-accessible">                 
                                  Submit</label>
                           <button onclick="this.form.submit()" value="Submit" 
                                   data-inline="true"></button> 
                           <span class="error" id="login_err" name="login_err" 
                                 style="display: none; font-size: 12px; 
                                 text-align: center;">status</span>
                       </div>                
                  </div>
             </fieldset>
        </form>
    </div>
</div>

js代码

    // AJAX Call handler using method="POST"
    function ajaxPostCallManip( str, url, toDoFunc )
    {   
        var xmlhttp;                // Request variable
        if( window.XMLHttpRequest )         // For modern browsers
        xmlhttp = new XMLHttpRequest;
        else                    // For old browsers
        xmlhttp = new ActiveXOBject("Microsoft.XMLHttp");

        xmlhttp.onreadystatechange=toDoFunc;
        xmlhttp.open("POST", url, true);
        xmlhttp.send(str);
    }

    function check_login()
    {
        // Construct the POST variables [username, password]
        var postStr = "username=" + $('#login_username').val() + "&" 
            + "password=" + $('#login_pwd').val();

        // Call the general purpose AJAX Handler Function
        ajaxPostCallManip(postStr, "login.php", function()  
        // toDoFunc to be performed when server response is ready
        {
        if( xmlhttp.readyState == 4 && xmlhttp.status == 200 )
        {   
            alert(xmlhttp.responseText);        
            switch(xmlhttp.responseText)
            {
                case "1":
                $('#login_err').css({'color':'green','display':'block'})
                                       .html('Successful Login');                   
                break;

            case "2":
                    $('#login_err').css({'color':'red','display':'block'})
                                       .html('incorrect username/password')
                break;

            case "3":
                $('#login_err').css({'color':'red','display':'block'})
                                       .html('please fill in all fields')
                break;
            }
        }
        });
    }

PHP代码

<?php
    include('dbManip.php');

    echo '<script> alert("Inside login.php"); </script>';

    $username = $_POST['username'];
    $password = $_POST['password'];

    // Just to check that the POST variables arrived safely
    echo "<script> alert('username = ' + $username); </script>";    


    if(!empty($username) && !empty($password))
    {
        // Fetch data from database
        $query = "
            SELECT username,password
            FROM users
            WHERE username = '$username' and password = '$password';
        ";  

        // Execute query
        $res = mysql_query($query);

        // If there is a match for the credentials entered with the database
        if(mysql_num_rows($res) == 1)
        {
            // Fetch information and double check credentials
            while($row = mysql_fetch_assoc($res))
            {
                $db_username = $row['username'];
                $db_password = $row['password'];
            }

            // Compare results with user input
            if( $username == $db_username && $password == $db_password)
            {
                // Credentials are correct - response = 1
                echo '1';
            }
            else 
            {
                // Credentials are incorrect - response = 2
                echo '2';
            }
        }
        else
        {
            // There is no match in the database
            // Credentials are incorrect - response = 2
            echo '2';
        }
    }   
    else 
    {
        // If one or both fields are empty - response = 3
        echo '3';
    }
?>

共有3个答案

甄华清
2023-03-14

从表单标记中删除method=“post”action=“属性,因为您是在ajax调用中定义它的

卢锋
2023-03-14

我认为你需要重写这行代码:

<form method="POST" action="" onsubmit="check_login(); return false;">

并将其更改为:

<form method="POST" action="login.php" onsubmit="check_login(); return false;">

确保保存登录名。php与html文件位于同一文件夹中。

罗业
2023-03-14

首先,您需要重写提交按钮:

<button type="Submit" value="Submit"data-inline="true"></button> 

其次,将变量从函数中移动,使其成为全局:

    var xmlhttp;      // Request variable 
    function ajaxPostCallManip( str, url, toDoFunc )
    {   
        if( window.XMLHttpRequest )         // For modern browsers
        xmlhttp = new XMLHttpRequest;
        else                    // For old browsers
        xmlhttp = new ActiveXOBject("Microsoft.XMLHttp");

        xmlhttp.onreadystatechange=toDoFunc;
        xmlhttp.open("POST", url, true);
        xmlhttp.send(str);
    }
 类似资料:
  • 问题内容: 通过ajax提交登录表单时,我只是遇到一些问题,我主要是PHP开发人员,在PHP中我并不经常使用Jquery + Ajax。 目前,如果我在提交表单后检查firebug POST数据,它的确会获取已添加到表单的用户名和密码,但是无论是否添加了错误的用户名和密码,或者是否添加了错误的页面,页面都只会重新加载它们是正确的,并且没有创建会话。 形式如下: 这是Ajax / Jquery: 这

  • 问题内容: 我正在忙于开发一个网站,该网站的右上角有一个登录框。我希望用户能够在不刷新的情况下登录同一页面。 好的,我已经开始工作了,但是我仍在努力登录后的过程,如下所示: (HTML) (PHP) (我的jQuery) (isLoggedIn.php) (isNotLoggedIn.php) 登录过程可以正常工作,但是注销混乱。当我单击isLoggedIn.php中的Logout链接时,它注销了

  • 我需要用PHP和SQlite做一个登录表单。注册有效,我可以选择所有创建的用户并回显它们。当我试图检查用户名和密码(我通过$u POST获得的)是否匹配时:它回显我现在已登录。但当我输入错误的user/pw时,它会回显“invalid”-字符串,但也有以下错误: 第17行是我得到if($row['username']的地方... 这是我的代码: 这里怎么了?

  • 问题内容: 我有一个示例,尝试使用Symfony2和FOSUserBundle创建AJAX登录。我设置我自己和在我的文件。 这是课程: 这对于处理成功和失败的AJAX登录尝试都非常有用。但是,启用后-我无法通过标准表单POST方法(非AJAX)登录。我收到以下错误: 我希望自己的重写和重写仅对XmlHttpRequests(AJAX请求)执行,如果没有,则将执行交还给原始处理程序。 有没有办法做到

  • 我将使用CSRF codeigniter和ajax进行安全登录。但是我有一个问题,我的语法。 这是我的表格: 这是我的javascript: 和我的控制器: 我的模型是:

  • 问题内容: 我刚刚启动PHP和mySQL,需要知道这是否“安全”。登录信息通过AJAX(jQuery)传递到以下PHP文件中。 jQuery AJAX 的PHP 我知道这可能不是做事的最佳方法,但我知道!我只想知道它是否有任何重大的安全缺陷。对我来说,这更多是一个概念,因此我没有合并SSL。 问题答案: 您应该进行此更改,以防万一密码中出现反斜杠: 首先,md5非常糟糕。也和是多余的。在野外产生了