向表单PHP HTML添加URL参数

时间:2022-08-24 11:51:12

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

在表单动作中放置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>

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

我更喜欢URL而不是读

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

3 个解决方案

#1


14  

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

在我看来,你应该将它们添加为隐藏字段。如果可以通过隐藏的表单字段进行传递,则没有必要尝试传递params

use that:

使用:

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

#2


3  

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:

A看不到任何你不能或不应该那样做的理由。不过,我比较喜欢的处理方法是:

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

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

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.

我通常自己使用post,但我确信get也会这样工作。希望这能回答你的问题。

#3


2  

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:

我认为和Teodor一样,没有理由不将变量作为隐藏字段发送。但如果你有充分的理由去做的话……您是否尝试过在url的末尾添加一个&:

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

#1


14  

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

在我看来,你应该将它们添加为隐藏字段。如果可以通过隐藏的表单字段进行传递,则没有必要尝试传递params

use that:

使用:

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

#2


3  

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:

A看不到任何你不能或不应该那样做的理由。不过,我比较喜欢的处理方法是:

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

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

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.

我通常自己使用post,但我确信get也会这样工作。希望这能回答你的问题。

#3


2  

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:

我认为和Teodor一样,没有理由不将变量作为隐藏字段发送。但如果你有充分的理由去做的话……您是否尝试过在url的末尾添加一个&:

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