【问题标题】:Search function with get and selector带有 get 和 selector 的搜索功能
【发布时间】:2014-05-14 12:00:50
【问题描述】:

对不起,如果标题不完全正确,但我不确定我到底想要什么(好吧,如何用英语说出来)。

基本上我有一个输入字段,有人可以在其中输入一些文本并按下回车/按钮来搜索数据库。 该表格将他重定向到 site.com/search.php?search=my+dumb+text 但我想有一个选择器(它将是下拉选择),所以他们可以按标题或用户查找,我希望搜索 URL 看起来像这样:

site.com/search.php?user=dumb+name site.com/search.php?title=weird+title

只是为了澄清:我不希望 URL 是:site.com/rearch.php?user=dumb+name&type=user 否则我就不必在那里问了。

是否可以不使用 JS 或将表单作为 POST 发送,然后将它们重定向到 search.php 文件?

这就是现在的样子:

<div class='search'>
    <form method='get' action='search.php'>
    <select name='type'>
        <option value='title'>Title IMG</option>
        <option value='user'>User IMG</option>
    </select>
    <input type='text' name='search' value='$search' placeholder='Search' class='search-field'><input type='submit' value='s' class='search-button'>
    </form>
</div>

【问题讨论】:

  • 你的意思是像site.com/search.php?user=dumb+name&amp;title=weird+title这样的东西吗?
  • 旁注如果 value='$search' 来自 PHP(假设因为到处都有 ' 引号),那么您可以打开 XSS attack
  • Jurik:这就是我不想要的。我希望 URL 是 user=something 或 title=something,而不是两者。 Loz:基本上我需要 HTML 方面的帮助,只是忽略那里的 PHP 东西,这与我的需求无关 :)
  • @MiChAeLoKGB 我不完全理解你为什么要强制 URL 像这样,你能解释一下你为什么这样做吗?也许您可能想研究重写规则?尽管如果没有 JS,您仍然无法让 HTML 表单按您的意愿工作。
  • 它主要用于 URL 的外观。一切正常,我只是想让它看起来更好。

标签: php .htaccess search get


【解决方案1】:

你想要的可能是一个收音机,让用户选择他想要的搜索类型:

<input type="radio" name="type" value="user">User<br>
<input type="radio" name="type" value="title">Title

这样您会得到类似 ?search=A+B+C&type=user 的查询

这样比将“搜索”参数更改为其他参数更灵活

没有js是无法动态改变提交表单的路径或者表单中的字段名的。

【讨论】:

  • 这正是我不想要的。我忘了提。
  • 那么正如我在答案中提到的,没有js是不可能的,句号。
  • 好的,所以你刚刚确认了我的建议。我不想使用 JS,所以也许发布和重定向。或两者兼有(js 为主,post 为备份)。
【解决方案2】:
<div class='search'>
<?php echo "<form method='get' action='search.php?title= ".input_filter(INPUT_GET, 'search');." '>" ?>
<select name='type'>
    <option value='title'>Title IMG</option>
    <option value='user'>User IMG</option>
</select>
<input type='text' name='search' value='$search' placeholder='Search' class='search-field'><input type='submit' value='s' class='search-button'>
</form>
</div>

试试这个代码,让我知道它是否有效:D。

【讨论】:

  • 我什至不必尝试就知道它不起作用。我希望 TITLE 部分是动态的,并且取决于用户在下拉菜单中选择的内容。所以会有标题或用户,并不总是只有一个。
【解决方案3】:

正如其他人指出的那样,我希望这不是真的:没有重定向是不可能的,所以我使用 .htaccess 来完成它。

表格代码如下:

<div class='search'>
    <form method='get' action='search.php'>
        <select name='type' class='search-select'>
            <option value='replay' ".(isset($_GET['replay']) ? 'selected' : '').">Replays</option>
            <option value='player' ".(isset($_GET['player']) ? 'selected' : '').">Users</option>
        </select>
        <input type='text' name='search' value='$search' placeholder='Search' class='search-field'>
        <input type='submit' value='s' class='search-button'>
    </form>
</div>

.htaccess 版本

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING} ^type=player&search=(.*)$ [NC]
RewriteRule ^search.php$ /search.php?player=%1 [L,R=301]
RewriteRule ^search.php?player=%1$ /search.php [L]

RewriteCond %{QUERY_STRING} ^type=replay&search=(.*)$ [NC]
RewriteRule ^search.php$ /search.php?replay=%1 [L,R=301]
RewriteRule ^search.php?replay=%1$ /search.php [L]

PHP 版本

if (isset($_GET['type'])){
    if ($_GET['type'] == "player"){
        header("location:/search.php?player=".$_GET['search']);
        exit();
    }elseif($_GET['type'] == "replay"){
        header("location:/search.php?replay=".$_GET['search']);
        exit();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多