Bad value for attribute action in element form: must be nonempty

I have a regular form in my php file, after it is submitted, it needs to echo messages. By putting something in action = "", the only way I can think of displaying a message is to save it in the session and display it when the page loads, if there is a set of sessions.

Everything works fine, as it is now, but the w3c validator says that I have an error:

Bad value for attribute action in element form: must be nonempty.

How can I fix this error without putting # or index.php in the action field?

EDIT:

<form action="" method="post"> <input type="email" name="email" placeholder="Enter your email"> <input type="submit" name="submit" value="SEND"> </form> <?php if(isset($_POST['submit']) && isset($_POST['email'])){ if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){ echo 'This isn\'ta valid email'; }else{ echo 'Great'; } } ?> 
+6
source share
2 answers

Supporting W3C HTML Checker (validator) here. If your goal is to make the control panel not emit this error, one way to do this is to go and put # as the value for the action attribute in the HTML source code, but then delete it using JavaScript, for example:

 <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> 
+6
source

When using the W3C validator https://validator.w3.org/ , the following was presented:

Row 1, column 1: document type declaration; will be analyzed without verification

The document type could not be determined because the document did not have the correct DOCTYPE declaration. The document is not like HTML, so automatic rollback cannot be performed, and the document is checked only for the syntax of the basic markup.

Learn how to add doctype to a document from our FAQ or use the Document Type for Authentication parameter to check your document for a specific type of document.

  • Along with a few others, but I did not include them here.

Decision:

You need to declare valid doctype and <head><title> tags, as this will also lead to errors if they are omitted.

Then use action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" , As I said in the comments.

Now your code will be checked as follows:

 <!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\'ta valid email'; }else{ echo 'Great'; } } ?> 
  • Sidenote: you can have PHP on top, both methods checked correctly.

Using action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" , You created the following / similar in the HTML source:

<form action="/folder/file.php" method="post">

While omitting this, you created action="" , which the W3 validator appears to have found invalid and is looking for a valid file for the form action.


Edit:

In light of the recently accepted answer (mine unaccepted), note that your code will not work properly if Javascript is disabled.

+6
source

All Articles