how u doing?
I using this PHP validation below to send a http response to a javascript function, but I’m getting the error “TypeError: Cannot read properties of undefined (reading ‘trim’)”.
Can anyone help me with to identify where should a change to fix this typeerror?
PHP
if(!$validate_status) {
header($_SERVER["SERVER_PROTOCOL"]." 400 Bad Request");
} else {
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
}
Javascript
function validate_submit(thisForm, action, formData) {
fetch(action, {
method: 'POST',
body: formData,
headers: {'X-Requested-With': 'XMLHttpRequest'}
})
.then(response => {
if( response.ok ) {
thisForm.querySelector('.loading').classList.remove('d-block');
thisForm.querySelector('.sent-message').classList.add('d-block');
thisForm.reset();
} else {
throw new Error(`${response.status} ${response.statusText} ${response.url}`);
}
})
.then(data => {
thisForm.querySelector('.loading').classList.remove('d-block');
if (data.trim() == 'OK') {
thisForm.querySelector('.sent-message').classList.add('d-block');
thisForm.reset();
} else {
throw new Error(data ? data : 'Form submission failed and no error message returned from: ' + action);
}
})
.catch((error) => {
displayError(thisForm, error);
});
}
I tried to debug with chrome dev tools, but with no success. I couldn’t find out why data content sent from php is undefined and I don’t know how to fix it.