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

4

10

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';
        }
    }
?>
Masterly answered 10/9, 2015 at 0:56 Comment(11)
scratch that >> aren't you using <form action="" right now? << Edit: You can also try action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" if your form isn't already set inside PHP.Indohittite
It's a little unclear what you're working with. Can you post more of the code? Where is the code you're targeting, on the same page?Fraze
@Fred-ii- yes i am using action="" right now, and the error says that is cannot be empty.Masterly
edited my question. the echo 'great' does not show up as the action takes the user to a "new" placeMasterly
lol of course. i just copied & pasted quickMasterly
what is your <!DOCTYPE top of the php file for the w3c validatorRap
look here for a valid dtd.Rap
@Masterly I upvoted your question not because of the answer I gave (edit: and you now un-accepted), but because you posted a question (and to be honest) that I was unaware of about the W3C validator regarding it erroring on action="". I feel it is a good addition to Stack's archive.Indohittite
@Masterly I guess I should take that back then; you decided to unaccept. You have your full solution then. However, if JS is every disabled, your code will no longer function. I thought you need to know that.Indohittite
that is a very valid point. The solution you gave me made the form not work at all, it seemed like it just refreshed the page. I didn't unaccept because of what you said, i just liked the other answer as it worked. If you can provide me with a similar solution without js, ill be happy to see it!Masterly
@Masterly if you were to show us your full code, then I'll be able to help. I can't see how what you posted originally would make your code fail. Again, I posted an answer for what you posted. You left out important parts of it and I for one can't guess what your full/real/actual code looks like or functions.Indohittite
A
16

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>
Anastaciaanastas answered 10/9, 2015 at 1:31 Comment(0)
I
5

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.

Indohittite answered 10/9, 2015 at 1:23 Comment(10)
I do have the <head> and <title> already. The only problem with this is that the echo 'Great'; is not showing up after the submission. The page just refreshes and that's allMasterly
@Masterly : you are asking about : Everything works fine the way it is right now but w3c validator says I have an error:Rap
yeah so if I do what you suggested, the error goes away but "everything" wont work fine anymore as the echo message won't appear like i want it to. I guess storing the message into a session is the only way around this errorMasterly
@Masterly worked fine for me. Is the file .php extension? Running off a local machine or hosted?Indohittite
@Masterly what do you mean by "won't appear like I want it to" ? how/where do you want it to appear?Indohittite
i have an echo that calls a div to appear on the screen upon successful submission of the form. doing it with your suggestion, make nothing happen - just a page refreshMasterly
@Masterly I posted an answer for what you originally posted. I'd say I answered the original question. If you have another question about the div, then you should post a new one.Indohittite
that's okay. i think just putting action="#" works although it didnt work for me at other pages, weird. anyways, thanks for your help!Masterly
@Masterly You're welcome. If you do plan on posting a new question, please make sure that you post your entire (relevant) code. If you have some JS/Ajax happening, then that could be an added factor. Check for errors and check your console.Indohittite
I had to use $_SERVER["REQUEST_URI"] (@Tama solution): I use apache mod-rewrite, and $_SERVER["PHP_SELF"] contains the re-written URI, not the original one.Periphrasis
H
3

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.

Hypochondriac answered 15/6, 2018 at 20:48 Comment(0)
M
1

For me the solution was to remove the empty action attribute. The form still works and the validation error is gone.

Metaplasm answered 3/4, 2020 at 13:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.