2014-12-05 29 views
0

讓我們3個關鍵字:陽光日食如何插入搜索查詢字符串中的錨

當使用形式進行搜索:

<form method="get" action="search.php"> 
    <input name="q" type="search"> 
</form> 

網址看上去很漂亮,像這樣:

http://localhost/gallery/search.php?q=sunlight+mirror+eclipse 

到目前爲止 太好了。

當試圖插入搜索字符串的錨,這樣

// get the query string 
if (isset($_GET['q'])) { 
    $current_q = $_GET['q']; 
} 

<a href="<?php echo 'http://localhost/gallery/search.php?q=' . $current_q; ?>">this is my query</a> 

我得到weirds人物醜陋的東西在關鍵字之間,就像這樣:

http://localhost/gallery/search.php?q=sunlight%20mirror%20eclipse 

爲什麼?如何使它更好,就像使用表單一樣?

感謝

回答

0

做到這一點是使用$_SERVER['QUERY_STRING']? 在此之後會得到什麼,這將得到它以同樣的方式將網址是得到了

http://localhost/gallery/search.php?q=sunlight+mirror+eclipse 

一些代碼

最簡單的方法
<?php 
$query = $_SERVER['QUERY_STRING']; 
$query = str_replace(' ', '+', urldecode($query));  
echo "<a href='http://localhost/gallery/search.php?$query'>this is my query</a>"; 

Documentation PHP SERVER MANUAL

+0

當有人downvote他可以解釋爲什麼至少 – codinginsane 2014-12-05 22:01:37

+0

嘗試它,但我得到關鍵字之間的怪異字符。順便說一句,我沒有投票。 – Marco 2014-12-05 22:03:53

+0

@Marco現在嘗試 – codinginsane 2014-12-05 22:08:20