我正在使用AJAX来处理我的ChangePassword类,它扩展了我的DataProcessor类。我从AJAX响应中得到的一切都是以某种方式在它之前添加了一个巨大的空白,可能相当于10个空格。以下是我的ChangePassword类:
<?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 ?>
接下来是我的DataProcessor类:
<?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 :(得分:0)
如果它只是空格,为什么不使用trim()?
你也应该修剪javascript
if (request.readyState == 4 && request.status == 200){
var return_data = request.responseText;
return_data = return_data.trim()