2013-07-30 192 views
0

我使用AJAX處理ChangePassword類,它擴展了我的DataProcessor類。我從AJAX響應中得到的所有內容都會以某種方式在它之前添加一個巨大的空白,可能相當於10個空格。以下是我的ChangePassword類:PHP額外的空白?

<?php 
require_once "../support/RequiredClasses.php"; 
class ChangePassword extends DataProcessor { 
    private $_old_password; 
    private $_new_password_1; 
    private $_new_password_2; 
    private $_password_enc; 
    //============================================================================================================================================ Class Constrcutor 
    public function __construct($_old_password,$_new_password_1,$_new_password_2){ 
     try { 
      $_DM      = new DataManager(); 
      $this->_conn    = $_DM->connect(); 
      $this->_old_password  = filter_var($_old_password, FILTER_SANITIZE_STRING); 
      $this->_new_password_1 = filter_var($_new_password_1,FILTER_SANITIZE_STRING); 
      $this->_new_password_2 = filter_var($_new_password_2,FILTER_SANITIZE_STRING); 
       if (!$_old_password OR !$_new_password_1 OR !$_new_password_2) 
                    {throw new Exception("You have not filled in all of the fields.");} 
       if ($this->_new_password_1 != $this->_new_password_2){throw new Exception("The two new passwords you entered do not match.");} 
       if (!isset($this->_new_password_1[7]))    {throw new Exception("Your new password must be at least 8 digits in length.");} 
      $this->_old_password_enc = $_DM->encrypt($this->_old_password,1,$_SESSION["user_email"]); 
       if (!$this->verifyPassword())      {throw new Exception("The old password you entered was incorrect.");} 
      $this->_new_password_enc = $_DM->encrypt($this->_new_password_1,1,$_SESSION["user_email"]); 
      $this->_access   = $this->makeChanges(); 
     } // end try 
     catch (Exception $_exception){$this->_errors[] = $_exception->getMessage();} 
    } // end __construct 
    public function ChangePassword(){$this->__construct($_old_password,$_new_password_1,$_new_password_2);} 
    //============================================================================================================================================ Changing Password 
    protected function makeChanges(){ 
     $_update = $this->_conn->prepare("UPDATE users SET password=? WHERE user_id=?"); 
      if (!$_update){return FALSE;} 
     $_update->bind_param("si",$this->_new_password_enc,$_SESSION["user_id"]); 
     $_update->execute(); 
     $_update->free_result(); 
     return TRUE; 
    } // end makeChanges 
    //============================================================================================================================================ Verifying Old Password 
    private function verifyPassword(){ 
     $_verify = $this->_conn->prepare("SELECT role_id FROM users WHERE user_id=? AND password=? LIMIT 1"); 
     $_verify->bind_param("is",$_SESSION["user_id"],$this->_old_password_enc); 
     $_verify->execute(); 
     $_verify->bind_result($_fetched_result); 
     $_verify->fetch(); 
     $_verify->free_result(); 
     return ($_fetched_result) ? TRUE : FALSE; 
    } // end verifyPassword 
} // end class ChangePassword 
if (isset($_POST["q"]) AND $_POST["q"] == 1){ 
    $_ChangePassword = new ChangePassword($_POST["old_password"],$_POST["new_password_1"],$_POST["new_password_2"]); 
    if (count($_ChangePassword->_errors)){echo $_ChangePassword->returnErrors();} 
    else { 
     echo "Success changing password."; 
    } // end else 
} // end if ?> 

下一個是我的數據處理器類:

<?php 
abstract class DataProcessor { 
    public $_conn;    // database connection 
    public $_access;   // whether or not the user has access 
    public $_errors = array(); // class-generated errors 
    //============================================================================================================================================ Returning a String of All Errors 
    public function returnErrors(){ 
     $_error_string = ""; 
     foreach ($this->_errors as $_error){$_error_string .= $_error;} 
     return $_error_string; 
    } // end returnErrors 
} // end abstract class DataProcessor ?> 

最後,我的AJAX代碼:

var request; 
prepareAJAX(); 
function prepareAJAX(){ 
request = new XMLHttpRequest(); 
} // end prepareAJAX 
function submitChangePassword(){ 
var old_password = document.getElementById("cp_old_password").value; 
var new_password_1 = document.getElementById("cp_new_password_1").value; 
var new_password_2 = document.getElementById("cp_new_password_2").value; 
var vars   = "q=1&old_password="+old_password+"&new_password_1="+new_password_1+"&new_password_2="+new_password_2; 
request.open("POST","../classes/class.ChangePassword.php"); 
request.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
request.onreadystatechange = function(){ 
    if (request.readyState == 4 && request.status == 200){ 
     var return_data = request.responseText; 
     alert(return_data); 
     var type = (return_data.charAt(0) == "1") ? "error" : "success"; 
     lightboxNotice("cp",type,return_data); 
    } // end if 
} // end function 
request.send(vars); 
} // end submitChangePassword 
function lightboxNotice(prefix,type,message){ 
switch(type){ 
    case "error": 
     document.getElementById(prefix+"_lightbox_notice").className = "ligthbox_notice lightbox_error"; 
     break; 
    case "message": 
     document.getElementById(prefix+"_lightbox_notice").className = "ligthbox_notice lightbox_message"; 
     break; 
    case "success": 
     document.getElementById(prefix+"_lightbox_notice").className = "ligthbox_notice lightbox_success"; 
     break; 
} // end switch 
document.getElementById(prefix+"_lightbox_notice").style.display = "block"; 
document.getElementById(prefix+"_lightbox_notice").style.visibility = "visible"; 
document.getElementById(prefix+"_lightbox_notice").style.opacity = 1; 
printNotice(prefix,type,message); 
} // end lightboxNotice 
function printNotice(prefix,type,message){ 
switch(type){ 
    case "error": 
     document.getElementById(prefix+"_notice").className = "notice_error"; 
     break; 
    case "message": 
     document.getElementById(prefix+"_notice").className = "notice_message"; 
     break; 
    case "success": 
     document.getElementById(prefix+"_notice").className = "notice_success"; 
     break; 
} // end switch 
document.getElementById(prefix+"_notice").style.display = "block"; 
document.getElementById(prefix+"_notice").style.visibility = "visible"; 
document.getElementById(prefix+"_notice").style.opacity = 1; 
document.getElementById(prefix+"_notice").innerHTML  = message; 
} // end printError 
+0

您的頁面上,您可能正在輸出一些html輸入文本和密碼字段通過ajax發送,對嗎?我猜想你有一些已經在輸入中的大量空白。但是,不要猜測,你應該發佈ajax正在處理的html,以便我們也可以看到。 – Zak

+1

這是很多代碼需要通過。你能解釋一下你爲調試而做出的努力嗎?你可以縮小問題的幾行代碼嗎? –

+1

在'<?php' – Barmar

回答

0

如果只有白色的空間,爲什麼不只是使用trim()?

你也應該修剪javascript

if (request.readyState == 4 && request.status == 200){ 
     var return_data = request.responseText; 
     return_data = return_data.trim() 
+0

之前檢查一堆空格是的,我試過trim()以及ltrim(),它們不起作用 – PHPman

+0

謝謝! – PHPman