帐户已更新,但我从未收到回复“ok” 在控制台中。相反,我得到的是意外的令牌<在JSON位于JSON.parse()的0位。
我根本无法发现错误。请帮我!我是绝望的事业,明天我参加考试,我在这个问题上挣扎了好几天。
我的HTML:
<!-- EDIT own account for USERS and ADMIN -->
<div id="pageEditAccount" class="page popup">
<div class="lblWrapper">
<h3>EDIT ACCOUNT</h3>
<form class="form" id="frmEditAccount">
<input type="hidden" name="txtEditAccountId" id="txtEditAccountId">
<input type="text" id="txtEditAccountEmailorPhoneNumber" name="txtEditAccountEmailorPhoneNumber" placeholder="Mobile number or Email">
<input type="text" id="txtEditAccountName" name="txtEditAccountName" placeholder="Name">
<input type="text" id="txtEditAccountLastName" name="txtEditAccountLastName" placeholder="Lastname">
<input type="password" id="txtEditAccountPassword" name="txtEditAccountPassword" placeholder="Password">
<img id="txtEditAccountImage" alt="AccountImage">
<div class="lblFileUpload">
<p>Select your picture:</p>
<input type="file" name="fileUserImage">
</div>
<button type="button" class="btnForm" id="btnEditAccountForm">Update</button>
</form>
<h3 class="lblErrorMessage" id="lblEditAccountErrorMessage"></h3>
</div>
</div>
我的JAVASCRIPT:
// sets value to the hidden field
function setInputValue() {
sUserId = sessionStorage.getItem( 'id' ); //ajUserDataFromServer.id;
document.getElementById( "txtEditAccountId" ).value = sUserId;
}
// Updates own account for ALL with POST
btnEditAccountForm.addEventListener( "click", editOwnAccount );
function editOwnAccount() {
setInputValue();
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
if ( this.readyState == 4 && this.status == 200 ) {
var ajUserDataFromServer = JSON.parse(this.responseText);
console.log( "Response:" + ajUserDataFromServer );
if ( ajUserDataFromServer.update == "ok" ) {
console.log( "ACCOUNT UPDATED" );
pageViewProducts.style.display = "flex";
//getajUserData(); //LOAD Users once again
getjUserData(); //UPDATE the cahnge
} else {
console.log( "UPDATE FAIL" );
pageEditAccount.style.display = "flex";
pageViewProducts.style.display = "none";
lblEditAccountErrorMessage.innerHTML = "";
var sEditAccountErrorMessage = "Updating Account Failed - Try again";
lblEditAccountErrorMessage.insertAdjacentHTML( 'beforeend', sEditAccountErrorMessage );
}
}
}
ajax.open( "POST", "api_edit_users.php", true );
var jFrmEditAccount = new FormData( frmEditAccount );
ajax.send( jFrmEditAccount );
}
我的PHP API:
<?php
//GETTING FROM FILE:
$sajUsers = file_get_contents('users.txt');
$ajUsers = json_decode($sajUsers);
//_________________________________________________________//
// getting from the front end:
$sUserId = $_POST['txtEditAccountId'];
$sNewUserEmailorPhoneNumber = $_POST['txtEditAccountEmailorPhoneNumber'];
$sNewUserName = $_POST['txtEditAccountName'];
$sNewUserLastName = $_POST['txtEditAccountLastName'];
$sNewUserPassword = $_POST['txtEditAccountPassword'];
$sNewUserImageUrl = $sFolder.$sFileName;
//_________________________________________________________//
$match_found = false;
// getting it from the database
for ( $i = 0; $i < count( $ajUsers ); $i++ ) {
if ( $sUserId == $ajUsers[$i]->id ) {
//checks if the value of the id from the front-end is equal to the value in the array.
if ( fnCheckEmailFormat ( $sNewUserEmail ) ) { // call the function which checks if is a valid email
unset($ajUsers[$i]->phonenumber); // if there is a phonenumber key delete it
$ajUsers[$i]->email = $_POST['txtEditAccountEmailorPhoneNumber']; // then assign an email key to the object user
}
else if ( fnCheckDigitFormat ( $sNewUserPhoneNumber ) ) { // call the function which checks that it should only contain digits
unset($ajUsers[$i]->email); //if there is an email key delete it
$ajUsers[$i]->phonenumber = $_POST['txtEditAccountEmailorPhoneNumber']; // then assign a phonenumber key to the object user
}
$ajUsers[$i]->name = $sNewUserName;
$ajUsers[$i]->lastname = $sNewUserLastName;
$ajUsers[$i]->password = $sNewUserPassword;
$ajUsers[$i]->image = $sNewUserImageUrl;
$match_found = true;
break;
}
}
//_________________________________________________________//
if( $match_found ) {
//PUTTING TO FILE:
$sajNewUsers = json_encode( $ajUsers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE );
file_put_contents('users.txt', $sajNewUsers);
echo $sjResponse = '{"update":"ok"}';
exit;
}
else {
echo $sjResponse = '{"update":"error"}'; // it didnt work.
exit;
}
//_________________________________________________________//
function fnCheckEmailFormat ( $sNewUserEmail ){ //checks if the property is valid. Called in line 6.
$sNewUserEmail = $_POST['txtEditAccountEmailorPhoneNumber'];
if ( !filter_var( $sNewUserEmail, FILTER_VALIDATE_EMAIL ) ){
return false; // returns false if its not valid. Then it wont run the if.
}
return true; // else it will run the signin.
}
function fnCheckDigitFormat ( $sNewUserPhoneNumber ){ //checks if the property is valid. Called in line 6.
$sNewUserPhoneNumber = $_POST['txtEditAccountEmailorPhoneNumber'];
if ( !preg_match( "/^[0-99]+$/", $sNewUserPhoneNumber ) ){
return false; // returns false if its not valid. Then it wont run the if.
}
return true; // else it will run the signin.
}
?>
现在我也知道该错误在以下代码中: 如果我成功删除帐户更新。
if ( fnCheckEmailFormat ( $sNewUserEmail ) ) {
unset($ajUsers[$i]->phonenumber);// call the function which checks if is a valid email
$ajUsers[$i]->email = $sNewUserEmailorPhoneNumber; // then assign an email key to the object user
}
else if ( fnCheckDigitFormat ( $sNewUserPhoneNumber ) ) { /*call the function which checks that it should only*/ unset($ajUsers[$i]->email); //contain digits
$ajUsers[$i]->phonenumber = $sNewUserEmailorPhoneNumber; // then assign a phonenumber key to the object user
}