2011-04-14 40 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