0

我正在尝试使用 ajax 和 php 构建登录系统。我使用日志视图,然后将数据保存在 ajax 中,这会带入我的 doLogin.php(php 文件)。我的问题是当我将 php 文件构建到一个类和一个函数中时,它永远不应该是任何 ajax 数据

看法:

public function DoLoginBox() {  
    //inloggning form-tagg...
    return '<p>&nbsp;</p>
        <div id="content">
          <h1>Login Form</h1>
          <form id="form1" name="form1" action="Handler/doLogin.php"  method="post">
            <p>
              <label for="username">Username: </label>
              <input type="text" name="username" id="username" />
            </p>
            <p>
              <label for="password">Password: </label>
              <input type="password" name="password" id="password" />
            </p>
            <p>
              <input type="submit" id="login" name="login" />
            </p>
          </form>
            <div id="message"></div>
        </div>';
}

阿贾克斯:

<script type="text/javascript">
 $(document).ready(function() {
$("#login").click(function() {
    var action = $("#form1").attr('action');
    var form_data = {
        username: $("#username").val(),
        password: $("#password").val(), 
        is_ajax: 1
    };
    $.ajax({
        type: "POST",
        url: action,
        data: form_data,
        success: function(response)
        {
            if(response == 'success')
                $("#form1").slideUp('slow', function() {
                    $("#message").html("<p class='success'>You have logged in successfully!</p>");
                });
            else
                $("#message").html("<p class='error'>Invalid username and/or password.</p>");   
        }
    });
    return false;
});
});
</script

PHP:

 <?php
 require_once ("UserHandler.php");
 class DoLogingHandler{

public function Login (){
    $is_ajax = !empty($_REQUEST['is_ajax']);

    if(isset($is_ajax) && $is_ajax)
    {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        $UserHandler = new UserHandler();
        $UserHandler -> controllDB($username,$password);

        if($username == 'demo' && $password == 'demo')
        {
            echo "success";

        }
    }
}

} ` $DoLogingHandler = new DoLogingHandler(); $DoLogingHandler->登录();?>


如果我在 stack.php 中添加类和函数,我会得到“连接到服务器时出错”(我希望将此代码放入一个类和函数中以调用其他函数:

<?php
class DologinHandler{  
public function Login(){ 
if(isset($_REQUEST['is_ajax']))
    {
        $username = $_REQUEST['username'];
        $password = $_REQUEST['password'];
        //$UserHandler = new UserHandler();
        //$UserHandler -> controllDB($username,$password);
        if($username == 'demo' && $password == 'demo')
        {
            exit('success');
        } else {
            exit('error');
        }
    }
}

} ?>

4

1 回答 1

0

用这个

第一个文件classes.php

<?php
class loginHandler{
    public static function doLogin(){
        if(!isset($_POST['is_ajax']) || !isset($_POST['username'])  || !isset($_POST['password'])){
            return false;
        }
        if(isset($_REQUEST['is_ajax']))
        {
            $username = $_REQUEST['username'];
            $password = $_REQUEST['password'];
            //$UserHandler = new UserHandler();
            //$UserHandler -> controllDB($username,$password);
            if($username == 'demo' && $password == 'demo')
            {
                exit('success');
            } else {
                exit('error');
            }
        }
    }
}
?>

第二个文件index.php

<?php
    require_once('classes.php');
    loginHandler::doLogin();//for static methods
    /*
        //or use this syntax for non static method
        $c = new loginHandler;
        $c->doLogin();
    */
?>
<html>
<head> <title>1</title>
<script type="text/javascript" src="jquery.js" ></script>  
</head>
<body>
<p>&nbsp;</p>
<div id="content">
  <h1>Login Form</h1>
  <form id="form1" name="form1" action="index.php"  method="post" onsubmit="return loginMe()">
    <p>
      <label for="username">Username: </label>
      <input type="text" name="username" id="username" />
    </p>
    <p>
      <label for="password">Password: </label>
      <input type="password" name="password" id="password" />
    </p>
    <p>
      <input type="submit" id="login" name="login"/>
    </p>
  </form>
    <div id="message"></div>
</div>
<script type="text/javascript">
    function loginMe(){
        var action = $("#form1").attr('action');
        var form_data = {
            username: $("#username").val(),
            password: $("#password").val(), 
            is_ajax: 1
        };
        $.ajax({
            type: 'POST',
            url: action,
            data: form_data,
            success: function(data){
                if(typeof(data) != 'undefined' && (data == 'success' || data == 'error')){
                    if($.trim(data) == 'success'){
                        $("#form1").slideUp('slow', function() {
                            $("#message").html("<p class='success'>You have logged in successfully!</p>");
                        });
                    } else if($.trim(data) == 'error'){
                        $("#form1").slideUp('slow', function() {
                            $("#message").html("<p class='error'>Invalid username and/or password.</p>");
                        });
                    }
                } else {
                    console.log(data);
                    $("#message").html("<p class='error'>Error to connect to server</p>");   
                }
            }
        });
        return false;
    }
</script>
</body>
</html>
于 2012-06-30T09:12:47.920 回答