Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从另一个类调用函数_Php_Ajax_Function_Jquery Mobile - Fatal编程技术网

php从另一个类调用函数

php从另一个类调用函数,php,ajax,function,jquery-mobile,Php,Ajax,Function,Jquery Mobile,我试图用另一个类的参数调用函数。带有我需要调用的函数的类是auth.class.php(它相当长,所以我缩短到了相关的类)。我有一个带有jQuery表单的index.php文件,当post调用reg.php并发送其数据时,该文件会显示。我想从reg.php调用auth.class.php文件中的register函数 我不确定我做错了什么,我认为ajax可能是问题所在,但任何帮助都将不胜感激 auth.class.php <?php class Auth { public functi

我试图用另一个类的参数调用函数。带有我需要调用的函数的类是auth.class.php(它相当长,所以我缩短到了相关的类)。我有一个带有jQuery表单的index.php文件,当post调用reg.php并发送其数据时,该文件会显示。我想从reg.php调用auth.class.php文件中的register函数

我不确定我做错了什么,我认为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;
            }
        }
    }
}
}

Make index.php所有代码都在这里,ajax函数也在这里,Make calls.php由ajax函数调用,calls.php文件调用class.auth.php文件

您没有将函数返回的值赋给任何对象

试试这个:

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

下面是修复该问题的更新代码。我没有以数组序列化的形式传递数据,还对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>

jquerymobile:主题下载
$(文档).ready(函数()
{
$(“#提交”)。单击(函数()
{
var formData=$(“#寄存器”).serializeArray();
$.ajax(
{
类型:“POST”,
url:“../pages/register.php”,
cache:false,
数据:formData,
成功:一旦成功,
});
返回false;
});
}); 
成功时的功能(数据、状态)
{
数据=$.trim(数据);
$(“#消息”)。文本(数据);
}
功能onError(数据、状态)
{
//处理错误
}
标题
电邮:

用户名:
密码:
确认密码
提交

新的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);

?>

echo$return<代码>echo$register->register($mail、$usn、$pswd、$cpswd)也许,在你出错之后…你的问题、错误、当前输出、期望的行为是什么?
  <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>
<?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);

?>