I have a registration form, on data.error PHP outputs as JSON the errors
exit('{"error": "Passwords do not match!"}');
After a successful register, the user will be redirected to exit('{"location": "home.php"}');
I want the json output, instead of plain text to be put into html+css for styling,
so i can style div for the errors
document
.querySelector(".register form")
.addEventListener("submit", async (e) => {
e.preventDefault();
const form = e.target;
const body = new FormData(form);
const res = await fetch(form.action, {
method: "POST",
body,
});
const data = await res.json();
if (data.error) { // If there is an error
document.querySelector("#msg").innerHTML = data.error;
}
else if (data.location) { // If we want to redirect the user somewhere
window.location.href = "./" + data.location;
}
});
If i add a class,
<div id="msg" class="error"></div> doesn't output anything,
while only inline style style="background-color:red;"> will work
.error {
background: #F2DEDE;
color: #A94442;
border-radius: 4px;
font-weight: bold;
text-align: center;
}
I been researching this for 12 hours, i've seen few examples about JSON.stringify and others,
but i don't know how could i implement this into my code

exit('{"error": "<div class="error">Passwords do not match!</div>"}');or style the element with idmsg. I really don't see where is the problem<div class="msg" style="background-color:red;"></div>and worked.