Bad value for attribute action on element form: Must be non-empty

I tried with

  <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">

but since in my case $_SERVER["PHP_SELF"] is always index.php (some rules in .htaccess do force this) the solution dosn't work.

In my case I had to use:

<form action="<?=htmlspecialchars($_SERVER["REQUEST_URI"]);?>" method="post">

This return to the same page you was before.


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.


Maintainer of the W3C HTML Checker (validator) here. If your goal is just to get the checker to not emit that error, one way to do that is to go ahead and put # as the value for the action attribute in your HTML source, but then remove it using JavaScript, like this:

<form action="#" method="post">
    <script>document.querySelector("form").setAttribute("action", "")</script>
    <input type="email" name="email" placeholder="Enter your email">
    <input type="submit" name="submit" value="SEND">
</form>