2014-08-27 38 views
0

我想從另一個類的參數調用一個函數。具有我需要調用的函數的類是auth.class.php(它很長,所以我縮短了相關的類)。我有一個的index.php文件jQuery的形式,當POST調用reg.php及發送數據。我想打電話給在auth.class.php寄存器功能從reg.php從另一個類的PHP調用函數

我不知道我做錯了,我認爲阿賈克斯可能是問題文件,但任何幫助將是讚賞。

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我使用jQuery Mobile的使用Ajax

<!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> 

這是我的PHP文件,該文件發出呼叫auth.class.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; 
?> 
+0

'echo $ return;'? echo $ register-> register($ mail,$ usn,$ pswd,$ cpswd);'也許,跟着你的錯誤... – Debflav 2014-08-27 13:10:53

+0

你的問題,錯誤,當前輸出,期望的行爲是什麼? – Sugar 2014-08-27 13:11:28

回答

0

做的index.php所有的代碼是在這裏和Ajax功能也是在這裏,讓calls.php這是AJAX的函數調用和calls.php文件調用您的class.auth.php文件。

0

您沒有將函數返回的值分配給任何東西。

試試這個:

$return= $register ->register($mail, $usn, $pswd, $cpswd); 
    echo $return; 
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); 

?>