将 URL 参数添加到表单 PHP HTML

发布于 2024-11-02 03:28:22 字数 580 浏览 1 评论 0原文

将 url get 参数放入表单操作中是否可以或正确?

<form method='get' action='index.php?do=search'>
  <input name='_search' type='text' value='What are you looking for?'>
  <button type='submit'> Search </button>
</form>

当我提交表单时,URL 更改为:

index.php?_search=What are you looking for? (I've stripped %20)

我希望读取 URL

index.php?do=search&_search=What are you looking for?

最好在表单中添加隐藏字段吗

<input type='hidden' name='do' value='search' />

Is it okay or correct to put a url get parameter in a form action?

<form method='get' action='index.php?do=search'>
  <input name='_search' type='text' value='What are you looking for?'>
  <button type='submit'> Search </button>
</form>

When I submit the form the URL is changed to:

index.php?_search=What are you looking for? (I've stripped %20)

I'd prefer the URL to read

index.php?do=search&_search=What are you looking for?

Would it be best to add a hidden field into the form

<input type='hidden' name='do' value='search' />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抱猫软卧 2024-11-09 03:28:22

在我看来,您应该将它们添加为隐藏字段。如果您可以通过隐藏表单字段来传递参数,则尝试传递参数是没有意义的

<input type='hidden' name='do' value='search' />

In my opinion you should add them as hidden fields. There is no point to try to pass params if you can do it via hidden form field

use that:

<input type='hidden' name='do' value='search' />
耀眼的星火 2024-11-09 03:28:22

我不明白为什么你不能或不应该那样做。然而,我首选的处理方法是:

<form method='get' action='index.php'>
    <input name='_search' type='text' value='What are you looking for?' />
    <submit name='do' value='Search'>
</form>

do/search 的名称/值对通过按下按钮传递,如果您想在表单上创建多个操作,则可以为每个提交按钮设置不同的值,处理以多种方式形成。

if ($_GET['do'] == "Search") {
 ... do Search ...
} else if ($_GET['do'] == "Foo") {
 ... do Foo ...
} else if ($_GET['do'] == "Bar") {
 ... do Bar ...
}

或者,您可以使用 case 构造:

switch($_GET['do']) {
    case "Search":
        ... do Search ...
    case "Foo":
        ... do Foo ...
        break;
    case "Bar":
        ... do Bar ...
        break;
} 

我自己通常使用 post ,但我确信 get 会以相同的方式工作。希望能回答您的问题。

A don't see any reason why you can't or shouldn't do it that way. My preferred method of handling it however would be:

<form method='get' action='index.php'>
    <input name='_search' type='text' value='What are you looking for?' />
    <submit name='do' value='Search'>
</form>

The name/value pair of do/search is passed through the button press, and if you want to create multiple actions on a form you can then have different values for each submit button, handling the form in multiple ways.

if ($_GET['do'] == "Search") {
 ... do Search ...
} else if ($_GET['do'] == "Foo") {
 ... do Foo ...
} else if ($_GET['do'] == "Bar") {
 ... do Bar ...
}

alternatively you can use a case construct:

switch($_GET['do']) {
    case "Search":
        ... do Search ...
    case "Foo":
        ... do Foo ...
        break;
    case "Bar":
        ... do Bar ...
        break;
} 

I normally use post myself, but I am sure get would work the same way. Hope that answers your question.

孤蝉 2024-11-09 03:28:22

我认为与 Teodor 一样,应该没有理由不将变量作为隐藏字段发送。但如果你有充分的理由这样做......你是否尝试过添加 &在网址末尾:

<form method='get' action='index.php?do=search&'>

I think the same as Teodor, there should be no reason to don't send the variable as a hidden field. But in case you have a good reason for doing that... Have you tried adding a & at the end of the url:

<form method='get' action='index.php?do=search&'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文