In using the W3C validator https://validator.w3.org/, was presented with the following:
Line 1, Column 1: no document type declaration; will parse without validation
The document type could not be determined, because the document had no correct DOCTYPE declaration. The document does not look like HTML, therefore automatic fallback could not be performed, and the document was only checked against basic markup syntax.
Learn how to add a doctype to your document from our FAQ, or use the validator's Document Type option to validate your document against a specific Document Type.
- Along with quite a few more, but I didn't include them here.
The solution:
You need to declare a valid doctype and <head><title>
tags, as this will also produce errors if omitted.
Then use action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
as I stated in comments.
Your code will now validate with the following:
<!DOCTYPE html>
<head>
<title>Test page</title>
</head>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<input type="email" name="email" placeholder="Enter your email">
<input type="submit" name="submit" value="SEND">
</form>
</body>
</html>
<?php
if(isset($_POST['submit']) && isset($_POST['email'])){
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
echo 'This isn\'t a valid email';
}else{
echo 'Great';
}
}
?>
- Sidenote: You can have the PHP on top also, both methods validated correctly.
Using action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
produced the following/similar in HTML source:
<form action="/folder/file.php" method="post">
While omitting it produced action=""
which the W3 validator seems to find invalid and is looking for a valid file for the form's action.
Edit:
In light of the newly accepted answer (mine being unaccepted), do note that your code will not work properly should Javascript be disabled.
<form action=""
right now? << Edit: You can also tryaction="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"
if your form isn't already set inside PHP. – Indohittite<!DOCTYPE
top of the php file for thew3c validator
– Rapaction=""
. I feel it is a good addition to Stack's archive. – Indohittite