如何将常量字符添加到 HTML 搜索表单的末尾

How to add a constant character to the end of a HTML search form

我有一个搜索表单,我必须让访问者使用通配符进行查询。我使用的脚本允许通过添加 ?* 进行通配符搜索,其方式与 Windows 搜索非常相似。尽管目前它可以正常工作,但没有人会在他的第一次尝试中在其查询末尾添加星号。

所以,我想知道是否可以以某种方式编辑我的 HTML 表单,以便自动让他们通过通配符进行搜索。例如,当他们搜索 "New York" 时,我希望我的页面也显示 "New Yorker" 的结果。查询 "new york*" 非常适合我的搜索表单,但正如我所解释的,我应该对它而不是用户进行排序,并让他们默认使用此功能。

<form action="index.php" method="get" style="margin:0" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">
<input class="input" name="q" type="text" value="" />
<input type="hidden" name="-1" value="0" />
<input type="hidden" name="strict" value="1" />
<input type="hidden" name="a" value="srch" />
<input type="submit" class="submitok" value="Search!" />
</td>
</form>

只需将事件侦听器附加到提交点击,并在提交表单之前在搜索查询的末尾添加一个星号...

document.getElementsByClassName("submitok")[0].addEventListener("click",function(){
   document.getElementsByClassName("input")[0].value+="*";
});
<form action="index.php" method="get" style="margin:0" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded">
    <input class="input" name="q" type="text" value="" />
    <input type="hidden" name="-1" value="0" />
    <input type="hidden" name="strict" value="1" />
    <input type="hidden" name="a" value="srch" />
    <input type="submit" class="submitok" class="submitok" value="Search!" />
</form>

点击侦听器代码执行后,将提交带有星号后缀的表单

PHP 将允许您在搜索字符串末尾添加缺失的 *,但我们需要您的 PHP 代码来提供帮助。

示例:

if(isset($_GET["q"]) && $_GET["q"] != "")
{
  $query = $_GET["q"] . "*";
}