将ajax数据转换为php文件

时间:2012-06-30 06:15:42

标签: php ajax

我正在尝试使用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>';
}

AJAX:

<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-&GT;登录();     ?&GT;


如果我在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');
        }
    }
}

}   ?&GT;

1 个答案:

答案 0 :(得分: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>