0

我正在尝试让一些代码为 un​​i 分配工作,我仍在学习,但我觉得我有点疯狂地试图理解为什么类变量不起作用。

使用这样的 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());
}
?>

但是,当我尝试使用下面的代码通过 javascript 调用使用它时,_login 变量无法更新,而对于我的一生,我无法弄清楚为什么?从这个链接可以看出; http://www.deakin.edu.au/~jtparker/SIT203/xyz/flower_shop2/myaccount.html 每次都失败?

任何想法在此先感谢杰伊

    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);
    }
}
4

1 回答 1

0

您只需调试 AJAX 调用的最终 URL 是什么,并确认是否所有内容都按预期发送,这将解决它。

尝试

console.log(submitString)

在您的 AJAX 调用之前,您将知道是否所有内容都已正确发送。

于 2013-09-24T04:16:27.560 回答