当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


HTML formmethod属性用法及代码示例

HTML formmethod 属性用于定义用于在提交表单时发送表单数据的 HTTP 方法。 GET 和 POST 是两种众所周知的 HTTP 方法。该属性覆盖了 <form> 元素的 method 属性的特性。

支持的标签:

用法:

<element formmethod="get|post">

值:

  • get:在 GET 方法中,提交表单后,表单值将在新浏览器选项卡的地址栏中可见。它的大小限制为大约 3000 个字符。它仅对非安全数据有用,对敏感信息无效。
  • post:在 post 方法中,提交表单后,表单值在新浏览器选项卡的地址栏中将不可见,因为它在 GET 方法中可见。它将表单数据附加到 HTTP 请求的正文中。它没有大小限制。此方法不支持为结果添加书签。

范例1:下面的代码演示了在 HTML <input type="submit"> 控件中使用 formmethod 属性。请参阅下面给出的输出以获得更好的理解。注意点击“Submit normally”和“使用POST方法提交”的新网页的地址栏。两者都不同,一个显示用户详细信息,后者隐藏所有用户详细信息。



HTML


<!DOCTYPE html>
<html>
<head>
    <title>
        HTML formmethod Attribute
    </title>
</head>
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>HTML formmethod Attribute</h3>
    <form action="#"
        id="users"
        action="#"
        method="GET"
        target="_blank">
        User_id:
        <input type="email"
            name="email"
            placeholder="Emter Email Id">
        <br>
        <br> Password:
        <input type="password"
            name="pword"
            placeholder="Enter Password">
        <br>
        <br>
        <input type="submit" value="Submit normally">
       
      <input type="submit" formaction="#"
             value="submit using POST method"
             formmethod="post">
    </form>
</body>
</html>

输出:

范例2:下面的示例演示了使用按钮元素的 HTML formmethod="get" 属性。它将用户条目提交到 “getDetail.php” PHP 文件。当用户单击“使用 get 方法提交”时,值将提交到 PHP 文件,注意下面输出中的地址栏。

HTML


<!DOCTYPE html>
<html>
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h3>HTML formmethod get Attribute</h3>
    <form action="getDetail.php">
        User name:
        <input name="username"
            placeholder="Enter username">
        <br>
        <br> Profession:
        <input
            name="profession"
            placeholder="Enter profession">
        <br>
        <br>   
       
      <button formmethod="get">            
             Submit using get method
      </button>
    </form>
</body>
</html>

输出:

支持的浏览器:

  • 谷歌浏览器 9.0
  • Internet Explorer 10.0
  • Firefox 4.0
  • Safari 5.1
  • Opera 10.6




相关用法


注:本文由纯净天空筛选整理自ManasChhabra2大神的英文原创作品 HTML formmethod Attribute。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。