2011-04-14 41 views
1

我在我的网站上有一个搜索引擎,SERP的网址是search/QUERY/1/搜索框到高级网址

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

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

+0

你将不得不使用Java脚本,这就是为什么它不是一个真正鼓励值得的IMO实践。 – 2011-04-14 22:40:23

+0

这里有一个很好的重复:[尼斯搜索URL](http://stackoverflow.com/questions/4890272/nice-search-urls) – 2011-04-14 22:43:21

回答

1

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 
?> 
+0

是的,你可以,但你必须把它放在index.php的顶部,为了更好的优化,把:exit(); affter标题行。 – 2011-04-15 11:40:05

+0

编辑我的答案,使用此 – 2011-04-15 11:44:41

5

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"> 
+0

@皮卡:谢谢。我认为浏览器将处理该功能。 :) – drudge 2011-04-14 22:55:18

+1

你应该可以用'+'替换'',所以你没有可怕的'%20'。 – Bonzo 2012-06-13 16:50:03