【问题标题】:Add Form field value to the URL on submit在提交时将表单字段值添加到 URL
【发布时间】:2015-09-11 18:25:02
【问题描述】:

我有一个表单,用户输入必须通过 URL 发送到另一个网站。

我试过这样:

<form onsubmit="window.location = 'http://example.com/en/' + city.value + '/apartments/listing/1?arrivalDate=' + from.value + '&departureDate=' + till.value + '&numPer=' + persons.value; return false; + '/' ">

但这会在同一个标​​签页中打开网页。还有其他方法可以实现这样的目标吗?也许用 PHP?

【问题讨论】:

  • 你可以试试 target="_blank"
  • 不幸的是,这不适用于 window.location。我尝试使用 window.open 但这不允许我在 URL 中添加表单字段。还是这样?
  • 为什么不使用GET 方法并设置操作。

标签: javascript php forms url


【解决方案1】:

form 使用GET 方法-

<form action="path_to_submit" method="GET" target="_blank">

【讨论】:

  • 这种方法是否允许我在新标签页中打开网站?
  • 是的。只需添加target="_blank"
【解决方案2】:

你可以这样写表格:

<form action="http://url/to/send/data/to.com" method="POST" target="_blank">
    <input type="text" name="name">
    <input type="text" name="email">
    <input type="text" name="message">
    <button type="submit">Submit</button>
</form>

“action”属性接受您希望发送表单数据的 url,“method”属性接受请求模式(GET 或 POST 等)。 'method' 默认设置为 GET,如果你愿意,你可以完全跳过它。如果设置为“_blank”,'target' 属性将打开一个新选项卡。

如果您需要进一步的帮助,请告诉我们。

【讨论】:

  • 1. 你怎么知道他需要POST 请求? 2.默认方法不是POST,而是GETmore info)。
  • 我在计数 2 上的错误。默认值确实是 GET。 #1。由于他提到“用户数据”将被发送到另一个“网站”——出于明显的基本安全原因,我认为他应该使用 POST。
【解决方案3】:

我用

解决了这个问题
<form onsubmit="window.open('http://example.com/en/' + place.value + '/apartments/listing/1?arrivalDate=' + from.value + '&departureDate=' + till.value + '&numPer=' + persons.value); return false;">

我不知道我可以将表单变量与window.open 一起使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多