简体   繁体   English

元素表单上属性操作的错误值:必须非空

[英]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.我的 php 文件中有一个常规表单,提交后它必须回显一条消息。 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.通过在 action="" 中放入任何内容,我能想到的显示消息的唯一方法是将其存储到会话中,并在页面加载时显示它(如果有会话集)。

Everything works fine the way it is right now but w3c validator says I have an error:现在一切正常,但 w3c 验证器说我有一个错误:

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?如何在不必将 # 或 index.php 放入操作字段的情况下修复此错误?

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';
        }
    }
?>

Maintainer of the W3C HTML Checker (validator) here. W3C HTML Checker(验证器)的维护者在这里。 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:如果您的目标只是让检查器不发出该错误,一种方法是继续将#作为 HTML 源中的action属性的值,然后使用 JavaScript 将其删除,如下所示:

<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>

In using the W3C validator https://validator.w3.org/ , was presented with the following:在使用 W3C 验证器https://validator.w3.org/ 时,显示如下:

Line 1, Column 1: no document type declaration;第 1 行,第 1 列:无文件类型声明; will parse without validation将在没有验证的情况下解析

The document type could not be determined, because the document had no correct DOCTYPE declaration.无法确定文档类型,因为文档没有正确的 DOCTYPE 声明。 The document does not look like HTML, therefore automatic fallback could not be performed, and the document was only checked against basic markup syntax.该文档看起来不像 HTML,因此无法执行自动回退,并且仅根据基本标记语法检查该文档。

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.您需要声明一个有效的 doctype 和<head><title>标签,因为如果省略这也会产生错误。

Then use action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" as I stated in comments.然后使用action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"如我在评论中所述。

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.旁注:您也可以将 PHP 放在最上面,两种方法都经过正确验证。

Using action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" produced the following/similar in HTML source:使用action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"在 HTML 源代码中产生以下/类似的内容:

<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.虽然省略它产生action="" ,W3 验证器似乎发现它无效,并且正在为表单的操作寻找有效的文件。


Edit:编辑:

In light of the newly accepted answer (mine being unaccepted), do note that your code will not work properly should Javascript be disabled.鉴于新接受的答案(我的未被接受),请注意,如果禁用 Javascript,您的代码将无法正常工作。

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.但由于在我的情况下 $_SERVER["PHP_SELF"] 始终是 index.php(.htaccess 中的某些规则确实强制这样做)该解决方案不起作用。

In my case I had to use:就我而言,我不得不使用:

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

This return to the same page you was before.这将返回到您之前所在的同一页面。

For me the solution was to remove the empty action attribute.对我来说,解决方案是删除空的action属性。 The form still works and the validation error is gone.表单仍然有效,验证错误消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM