Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a regular form on my php file, after it's submitted it has to echo a message. By putting anything in the action="", the only way I can think of displaying a message is by storing it into a session and display it when the page loads if there is a session set.

Everything works fine the way it is right now but w3c validator says I have an error:

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

How can I fix this error without having to put # or index.php into 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\'t a valid email';
        }else{
           echo 'Great';
        }
    }
?>
like image 836
BragDeal Avatar asked Sep 10 '15 00:09

BragDeal


3 Answers

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.

like image 97
Funk Forty Niner Avatar answered Nov 10 '22 15:11

Funk Forty Niner


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>
like image 37
sideshowbarker Avatar answered Nov 10 '22 14:11

sideshowbarker


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.

like image 37
Tama Avatar answered Nov 10 '22 16:11

Tama