1

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

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

我做了几个 alert()s 但没有出现...请帮助

代码

    <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代码

    // 使用 method="POST" 的 AJAX 调用处理程序
    函数 ajaxPostCallManip(str, url, toDoFunc)
    {   
        变量 xmlhttp; // 请求变量
        if( window.XMLHttpRequest ) // 对于现代浏览器
        xmlhttp = 新的 XMLHttpRequest;
        else // 对于旧浏览器
        xmlhttp = new ActiveXOBject("Microsoft.XMLHttp");

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

    函数 check_login()
    {
        // 构造 POST 变量 [用户名,密码]
        var postStr = "username=" + $('#login_username').val() + "&"
            + "密码=" + $('#login_pwd').val();

        // 调用通用AJAX Handler Function
        ajaxPostCallManip(postStr,“login.php”,函数()  
        // 当服务器响应准备好时执行 toDoFunc
        {
        如果(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {   
            警报(xmlhttp.responseText);        
            开关(xmlhttp.responseText)
            {
                情况1”:
                $('#login_err').css({'color':'green','display':'block'})
                                       .html('登录成功');                   
                休息;

            案例“2”:
                    $('#login_err').css({'color':'red','display':'block'})
                                       .html('用户名/密码错误')
                休息;

            案例“3”:
                $('#login_err').css({'color':'red','display':'block'})
                                       .html('请填写所有字段')
                休息;
            }
        }
        });
    }

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';
    }
?>
4

4 回答 4

0

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

<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);
    }
于 2013-07-18T14:53:20.773 回答
0

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

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

并将其更改为:

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

确保将 login.php 保存在与 html 文件相同的文件夹中。

于 2015-04-18T02:10:43.100 回答
0

从 form 标记中删除method="post"andaction=""属性,因为您是在 ajax 调用中定义它

于 2016-01-14T13:30:48.643 回答
-1
<button type="Submit" value="Submit"

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);
    }
于 2021-10-25T16:32:57.873 回答