HTML表单 - POST或GET取决于点击的按钮 [英] HTML form - POST or GET depending on button clicked

查看:272
本文介绍了HTML表单 - POST或GET取决于点击的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们有一个包含很多输入字段的搜索表单。用户可以点击:


  • [搜索]并获取结果列表,

  • [保存搜索条件]以供将来使用,并将表单内容发布到服务器上存储(我不想在本地存储在cookie中)。



我如何实现这一目标?我只找到解决方案,在两个单独的按钮上创建两种类型的POST请求。

也许JavaScript onclick函数设置< form method =??> 在提交之前?

解决方案<元素得到了一些新的属性

 < input type =submitformmethod =postformaction =save.phpvalue =Save/> 
< input type =submitformmethod =getformaction =search.phpvalue =Search/>

相同的属性是适用于<按钮>


Let there be a search form with quite many of the input fields. The user may either click:

  • [Search] and GET the list of the results,
  • [Save search criteria] for future use and POST the form content to be stored on server (I'm not interested in storing locally in cookies or so).

How do I achieve that? I've only found solutions to make two types of POST request on two separate buttons.

Maybe a JavaScript onclick function to set <form method="??"> right before its submitted? Would that work?

解决方案

In HTML5, the <input> element got some new attributes

<input type="submit" formmethod="post" formaction="save.php" value="Save" />
<input type="submit" formmethod="get" formaction="search.php" value="Search" />

The same attributes are valid for <button>

这篇关于HTML表单 - POST或GET取决于点击的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆