从另一个类调用php函数

时间:2014-08-27 13:07:44

标签: php ajax function jquery-mobile

我试图用另一个类的参数调用一个函数。具有我需要调用的函数的类是 auth.class.php (它很长,所以我缩短到相关的类)。我发布了一个带有jQuery表单的 index.php 文件,当发布 reg.php 并发送时数据他们的。我想从reg.php中 auth.class.php 文件中调用注册函数

我不确定我做错了什么,我认为ajax可能是问题,但任何帮助都将受到赞赏。

auth.class.php

 <?php
class Auth {

 public function register($email, $username, $password, $repeatpassword)
{
    $return = array();
    $return['code'] = 400;

    if ($this->isBlocked()) {
        $return['code'] = 0;
        return $return;
    } else {
        $validateEmail = $this->validateEmail($email);
        $validateUsername = $this->validateUsername($username);
        $validatePassword = $this->validatePassword($password);

        if ($validateEmail['error'] == 1) {
            $return['message'] = $validateEmail['message'];
            return $return;
        } elseif ($validateUsername['error'] == 1) {
            $return['message'] = $validateUsername['message'];
            return $return;
        } elseif ($validatePassword['error'] == 1) {
            $return['message'] = $validatePassword['message'];
            return $return;
        } elseif($password !== $repeatpassword) {
            $return['message'] = "password_nomatch";
            return $return;
        } else {
            if (!$this->isEmailTaken($email)) {
                if (!$this->isUsernameTaken($username)) {
                    $addUser = $this->addUser($email, $username, $password);

                    if($addUser['error'] == 0) {
                        $return['code'] = 200;
                        $return['message'] = "register_success";
                        return $return;
                    } else {
                        $return['message'] = $addUser['message'];
                        return $return;
                    }
                } else {
                    $this->addAttempt();

                    $this->addNewLog("", "REGISTER_FAIL_USERNAME", "User attempted to register new account with the username : {$username} -> Username already in use");

                    $return['message'] = "username_taken";
                    return $return;
                }
            } else {
                $this->addAttempt();

                $this->addNewLog("", "REGISTER_FAIL_EMAIL", "User attempted to register new account with the email : {$email} -> Email already in use");

                $return['message'] = "email_taken";
                return $return;
            }
        }
    }
}
}

这是我的html文件 index.php 我正在使用带有ajax的jQuery mobile

    <!DOCTYPE html>
<html>
    <head>
        <title>
            Submit a form via AJAX
        </title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.css" />
        <script src="http://code.jquery.com/jquery-1.5.2.min.js">
        </script>
        <script src="http://code.jquery.com/mobile/1.0a4/jquery.mobile-1.0a4.min.js">
        </script>
    </head>
    <body>
        <script>
            function onSuccess(data, status)
            {
                data = $.trim(data);
                $("#notification").text(data);
            }

            function onError(data, status)
            {
                // handle an error
            }

            $(document).ready(function()
                {
                    $("#submit").click(function()
                        {

                            var formData = $("#Register").serialize();

                            $.ajax(
                                {
                                    type: "POST",
                                    url: "reg.php",
                                    cache: false,
                                    data: formData,
                                    success: onSuccess,
                                    error: onError
                                });

                            return false;
                        });
                });
        </script>

        <!-- call ajax page -->
        <div data-role="page" id="callAjaxPage">
            <div data-role="header">
                <h1>
                    Call Ajax
                </h1>
            </div>

            <div data-role="content">
                <form id="Register">
                    <div data-role="fieldcontain">
                        <label for="Email">
                            eMail
                        </label>
                        <input type="email" name="email" id="email" value=""  />
                        <br />
                        <label for="UserName">
                            User Name
                        </label>
                        <input type="text" name="username" id="username" value=""  />
                        <br />

                        <label for="Password">
                            Password
                        </label>
                        <input type="password" name="password" id="password" value=""  />
                        <br />
                        <label for="Confirmpassword">
                            Confirm Password
                        </label>
                        <input type="password" name="repeatpassword" id="repeatpassword" value=""  />

                        <h3 id="notification">
                        </h3>
                        <button data-theme="b" id="submit" type="submit">
                            Submit
                        </button>
                    </div>
                </form>
            </div>

            <div data-role="footer">
                <h1>
                    GiantFlyingSaucer
                </h1>
            </div>
        </div>
    </body>
</html>

这是我的调用auth.class.php函数的php文件。

<?php

include_once "password.php";
require_once "auth.class.php";

$mail     = $_POST[email];
$usn      = $_POST[username];
$pswd     = $_POST[password];
$cpswd    = $_POST[repeatpassword];

$register = new Auth();
$register ->register($mail, $usn, $pswd, $cpswd);

echo $return;
?>

3 个答案:

答案 0 :(得分:0)

使index.php所有代码都在这里,ajax函数也在这里,并调用call.php,这是由ajax函数和calls.php文件调用你的class.auth.php文件。

答案 1 :(得分:0)

您没有将函数返回的值赋给任何东西。

试试这个:

  $return= $register ->register($mail, $usn, $pswd, $cpswd);
  echo $return;

答案 2 :(得分:0)

这是修复问题的更新代码。我没有将数据作为数组serializeArray传递,也在reg.php类中进行了一些更改。

index.php 文件

  <html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>jQuery Mobile: Theme Download</title>
    <link rel="stylesheet" href="themes/Carelincs.min.css" />
    <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile.structure-1.4.3.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
</head>
<body>

<script>

            $(document).ready(function()
                {
                    $("#submit").click(function()
                        {

                            var formData = $("#register").serializeArray();

                            $.ajax(
                                {
                                    type: "POST",
                                    url: "../pages/register.php",
                                    cache: false,
                                    data: formData,
                                    success: onSuccess,

                                });

                            return false;
                        });
                }); 


                function onSuccess(data, status)
            {

                data = $.trim(data);
                $("#message").text(data);
            }

            function onError(data, status)
            {
                //Handle error
            }

</script>



    <div data-role="page" data-theme="a">
        <div data-role="header" data-position="inline">
            <h1>Header</h1>
        </div>
        <div data-role="content" data-theme="a">
            <form action="" method="POST" id="register">
                <label for="email">Email:</label>
                <input type="email" name="email" placeholder="eMail"/>
                <br />
                <label for="username">Username:</label>
                <input type="text" name="username" placeholder="User Name"/>
                <br />
                <label for="password">Password:</label>
                <input type="password" name="password" placeholder="Password"/>
                <br />
                <label for="confirm password">Confirm Password</label>
                <input type="password" name="repeatpassword" placeholder="Confirm Password"/>
                <br />
                <button type="submit" id="submit">Submit</button>
            </form>

            <p id="message"></p>
        </div>
    </div>
</body>
</html>

reg.php 文件我还添加了连接数据库的信息。

<?php


require_once("../auth/config.class.php");
require_once("../auth/auth.class.php");

$config = new Config;

$dbh = new PDO("mysql:host=" . $config-> dbhost . ";dbname=" . $config->dbname,  $config->dbuser, $config->dbpass);


$auth = new Auth($dbh, $config);



$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
$repeatpassword = $_POST["repeatpassword"];


$register = $auth->register($email, $username, $password, $repeatpassword ); 

// Temporary just to see what's going on :
var_dump($register);

?>