2013-09-24 23 views
0

我試圖讓一些代碼爲uni任務工作,但我仍在學習,但感覺自己有點瘋狂,試圖理解類變量不工作的原因。PHP類變量無法通過XMLHttpRequest進行更新

使用PHP類,使得

class Users { 

    //Variables 
    protected $_userName; 
    protected $_password; 
    protected $_login; 
    protected $_firstName; 
    protected $_lastName; 
    protected $_email; 
    protected $_phone; 
    protected $_addressStreet; 
    protected $_addressCity; 
    protected $_addressState; 
    protected $_company; 

    public function __construct() { 
     // gets the number of parameters 
     $numArgs = func_num_args(); 
     $arg_list = func_get_args(); 
     // make decisions based on the arguments number 
     switch($numArgs){ 
      case "2":return $this->ConstructorWithTwoArgs($arg_list[0], $arg_list[1]); 
      case "10":return $this->ConstructorWithTenArgs($arg_list[0], $arg_list[1],$arg_list[2], $arg_list[3],$arg_list[4], $arg_list[5],$arg_list[6], $arg_list[7],$arg_list[8], $arg_list[9]); 
      default: 
       //Handle exception for method not existing with that many parrams 
       break; 
     } 
    } 
    //In order to log in we require minimum of user name and password 
    protected function ConstructorWithTwoArgs($userName, $password){ 
     $this->_userName = $userName; 
     $this->_password = $password; 
     $this->_login = "false"; 
    return $this; 
    } 
    //Checks users details and updates user details if valid 
    public function DoLogin(){ 
     $result = false; 
     // Check if userName and password exist in the db 
     $query = "SELECT * FROM SIT203Users WHERE USER_NAME = :userName AND PASSWORD = :password"; 
     // Create a new connection query 
     $ds = new Connection(); 
     $ds->parse($query); 
     $ds->setBindValue(':userName', $this->_userName); 
     $ds->setBindValue(':password', $this->_password); 
     $ds->execute(); 
     //User exists if rows are returned there will only be one as userName is unique 
     if($ds->getNextRow()){    
      $result = true; 
      $this->_login = "true"; 
      $this->_firstName = $ds->getRowValue("FIRST_NAME"); 
      $this->_lastName = $ds->getRowValue("LAST_NAME"); 
      $this->_email = $ds->getRowValue("EMAIL"); 
      $this->_phone = $ds->getRowValue("PHONE"); 
      $this->_addressStreet = $ds->getRowValue("ADDRESS_STREET"); 
      //Ensure all street details are obtained 
      if($ds->getRowValue("ADDRESS_STREET2")) 
       $this->_addressStreet .= $ds->getRowValue("ADDRESS_STREET2"); 
      $this->_addressCity = $ds->getRowValue("ADDRESS_CITY"); 
      $this->_addressState = $ds->getRowValue("ADDRESS_STATE"); 
      $this->_company = $ds->getRowValue("COMPANY"); 
    } 

     $ds->freeResources(); 
     $ds->close();  
     return $result;  
    } 
} 

現在這類工作得很好,從直接調用它; http://www.deakin.edu.au/~jtparker/SIT203/xyz/flower_shop2/MyAccount.php?userName=JaieP&password=jp

<?php 
require_once('initialise.php'); 

// Need to Validate all feilds and remove unwanted text 
// The feilds to be tested are the $_REQUEST values 
$userName = Validation::validateString(isset($_REQUEST['userName'])?$_REQUEST['userName']:""); 
$password = Validation::validateString(isset($_REQUEST['password'])?$_REQUEST['password']:""); 
$remberMe = isset($_REQUEST['remberMe'])?$_REQUEST['remberMe']:""; 

// Create a new user 
$newUser = new Users($userName, $password); 
// Try and login 
$newUser->DoLogin(); 
if($newUser->getLogin() == "true") 
    { 
    $_SESSION['user'] = $newUser; 
    // Echo out the users details plus cart details for last 3 months 

    //test its working to here 
    //echo($newUser->toString()); 
    echo("Its working!!!"); 
} 
else 
    { 
    //echo("falseValue"); 
    echo($userName.$password.$remberMe.$newUser->getLogin().$newUser->getUserName().$newUser->DoLogin().$newUser->toString()); 
} 
?> 

但是當我嘗試使用下面的代碼_login變量無法更新和我的生活,我不能工作,爲什麼出通過javascript調用使用它?從這個鏈接可以看出 ; http://www.deakin.edu.au/~jtparker/SIT203/xyz/flower_shop2/myaccount.html 它每次都失敗?

任何想法 提前感謝 Jaie

function TryLogin(){  
    try{ 
     // Get the userName and password supplied 
     var userName = document.getElementById("userName"); 
     var password = document.getElementById("password"); 

     // Get the remember me value 
     var rememberMe = document.getElementById("rememberMe"); 

     // Get the error feild 
     var errorDisplay = document.getElementById("submitError"); 
     // set to no error 
     errorDisplay.innerHTML = ""; 

     var documentMyAccount = document.getElementById("myAccount"); 

     // Submit details to server for verification if not empty 
     if(userName.value != "" && password.value != ""){ 
      // Now check via DB if username and password are valid 
      if (window.XMLHttpRequest) 
      { // code for IE7+, Firefox, Chrome, Opera, Safari 
       xmlhttp=new XMLHttpRequest(); 
      } 
      else 
      { // code for IE6, IE5 
       xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 

      xmlhttp.onreadystatechange=function() 
      { 
       if (xmlhttp.readyState==4 && xmlhttp.status==200) 
       { 
        // IF Response indicates a successful login 
        var myResponse = xmlhttp.responseText; 
        if(myResponse != "falseValue"){ 
         // set to nothing 
         documentMyAccount.innerHTML = ""; 
         // add php content 
         documentMyAccount.innerHTML = myResponse; 
        } 
        else{ 
         // Do not advise what details are incorrect, just that some combination is incorrect 
         errorDisplay.innerHTML = "Sorry those details are not correct"; 
        } 
       } 
      } 
      var submitString = "MyAccount.php?"; 
      submitString +="userName="+userName.value; 
      submitString +="&password="+password.value; 
      submitString +="&rememberMe="+rememberMe.checked?"true":"false"; 
      xmlhttp.open("GET",submitString,true); 
      xmlhttp.send(); 
     } 
     else{ 
      errorDisplay.innerHTML = "Not all details have been entered!"; 
     } 

    } 
    catch(error){ 
     alert(error.message); 
    } 
} 
+0

'console.log(submitString)'在xmlhttp.open'call之前的輸出是什麼? –

+0

真棒看到你的觀點,我認爲這個問題GET /~jtparker/SIT203/xyz/flower_shop2/MyAccount.php?userName=JaieP&password=jptrue放置一個答案,並會標記你正確,因爲你釘它,非常感謝:) – Jaie

回答

0

您只需調試那是什麼是被你的AJAX調用最終網址並確認,如果你希望被髮送的一切,這將解決它。

嘗試

console.log(submitString) 

您的AJAX調用之前,你就會知道,如果被正確發送的一切。

+0

很多謝謝!!!! – Jaie