我很抱歉,如果标题不完全正确,但我不确定我到底想要什么(好吧,如何用英语说出来)。
基本上我有一个输入字段,有人可以键入一些文本并按下输入/按钮来搜索数据库。 该表格将他重新发送到site.com/search.php?search=my+dumb+text 但我希望有一个选择器(它将是下拉选择),因此他们可以按标题或用户查找,我希望搜索URL看起来像这样:
site.com/search.php?user=dumb+name site.com/search.php?title=weird+title
只是为了澄清:我不希望网址为: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>
答案 0 :(得分:0)
你想要的是一个收音机让用户选择他想要的搜索类型:
<input type="radio" name="type" value="user">User<br>
<input type="radio" name="type" value="title">Title
通过这种方式,您可以获得如下搜索:search = A + B + C&amp; type = user
这种方式比改变&#34;搜索&#34;更灵活。参数到其他东西无法动态更改提交表单的路径或没有js的表单中的字段名称。
答案 1 :(得分:0)
<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。
答案 2 :(得分:0)
正如其他人指出我所希望的那样:没有重定向就不可能,所以我用.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>
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]
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();
}
}