PHP类变量无法通过XMLHttpRequest更新

时间:2013-09-24 03:44:26

标签: javascript php ajax

我正在尝试使用一些代码来完成单一任务,我仍然在学习但感觉我有点疯狂试图理解为什么类变量不起作用。

使用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 它每次都失败了吗?

任何想法 提前谢谢了 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);
    }
}

1 个答案:

答案 0 :(得分:0)

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

尝试

console.log(submitString)
在您的AJAX通话之前

,您将知道是否所有内容都正确发送。