I am doing a form validating using AJAX. If the user completes the form correctly, it will redirect to another page, else print out errors (plain HTML).
For example:
If there is an error in the form, the PHP file echos:
There is an error!
If there aren't any errors, the PHP file executes:
header("Location: http://example.com/successful"); exit();
But in AJAX, I am only handling for the plain HTML response:
success: function(response) {
_desireHTMLelment.innerHTML = response;
}
I know there is a way using json_encode()
, however, I still cannot classify plain HTML and JSON
.
Are there any ways I can do this? Thanks for any support!
Edit: I am using Vanilla JavaScript
This is how the system works:
User fills in the form => AJAX sends requests => PHP processes the request => Either returns an plain HTML error or redirects the page => AJAX receives the response, classify whether it is an error or HTML error => Find a correct way to handle the response