【问题标题】:Search box to advanced URL高级 URL 的搜索框
【发布时间】:2011-04-14 22:32:52
【问题描述】:

我的网站上有一个搜索引擎,SERP 的 URL 是 search/QUERY/1/

如何制作一个 HTML 搜索框来填写我的 URL 的 QUERY 部分?

它不能真正使用 PHP,但如果必须,它可以使用 javascript。

【问题讨论】:

  • 您必须使用 Javascript,这就是为什么它不是真正值得鼓励的 IMO 实践。
  • 这是一个很好的副本:Nice search URLs

标签: javascript html search


【解决方案1】:

使用 jQuery:

Live Demo

$('#search_button').click(function() {
    var query = encodeURIComponent($('#search_query').val());
    var searchurl = 'http://www.mysite.com/search/{query}/1/';
    var newurl = searchurl.replace('{query}',query);
    alert(newurl);
    // $(location).attr('href',newurl);
});

<input type="text" id="search_query">
<input type="button" id="search_button" value="Search">

【讨论】:

  • @Pekka:谢谢。我假设浏览器会处理该功能。 :)
  • 您可能应该将 ' ' 替换为 '+',这样您就不会出现可怕的 '%20's。
【解决方案2】:

HTML:

<form action="index.php" method="post">
<input type="text" name="word" />
<button type="submit">Search</button>
</form>

index.php:

<?PHP
if( isset( $_POST['word'] ) )
{
    header( 'location: /search/' . $_POST['word'] . '/' );
    exit();
}
//Other index.php codes
?>

【讨论】:

  • 是的,你可以,但你必须把它放在 index.php 的顶部,为了更好地优化,放:exit();在标题行之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 2012-03-11
  • 2018-07-13
  • 2015-04-17
  • 2012-04-09
  • 2015-04-06
  • 2018-11-05
相关资源
最近更新 更多