2013-04-27 50 views
0

我有以下報價:PHP獲取表單問題

<form class="searchform" name="search" id="search" action="<?php echo base_url();?>video/search" method="get" > 
       <input type="text" name="search_key" onblur="if (this.value == '') {this.value = 'Search video';}" onfocus="if (this.value == 'Search video') {this.value = '';}" value="<?php if(isset($search_key))echo $search_key;else echo 'Search video';?>" class="searchfield" onKeyPress="return submitenter(this,event)"> 
     </form> 

頁面的URL看起來像這樣搜索之後 - 例如:

mypage.com/video/search?search_key=blabla 

而且我使用,以顯示它的代碼關是:

<?php echo $_GET["search_key"]; ?> 

但它不工作,我不明白爲什麼。

我不能讓-blabla-在頁面顯示出來。

+1

是'mypage.com /視頻/ search'一個PHP文件? – Sean 2013-04-27 02:49:53

回答

0

actionform標籤,應該像myphp.php一個PHP文件,那麼你可以使用$_GET[ [「SEARCH_KEY']; at myphp.php`得到它。例如

action="<?php echo base_url();?>video/search/myphp.php" 

那麼URL想mypage.com/video/search/myphp.php?search_key=blabla

0

看一看,看看是否有任何價值在$_GET超級全球所有使用var_dump($_GET)將轉儲對象到屏幕的一個不那麼漂亮的字符串。

也有一個嘗試$_REQUEST$GLOBALS變量,看看他們是否有任何價值做同樣的上述。

如果這些沒有顯示您的值,您可能需要強制設置表格的enctype,以便PHP知道它可以將查詢字符串(從?開始的文本)或POST數據轉換爲超級全局變量。 enctype你想要使用的是application/x-www-form-urlencoded這是meant to be the default value但爲了以防萬一,請設置它。

有了它設置,你的表格看起來就像

<form class="searchform" name="search" id="search" action="<?php echo base_url();?>video/search" method="get" enctype="application/x-www-form-urlencoded"> 
    <input type="text" name="search_key" onblur="if (this.value == '') {this.value = 'Search video';}" onfocus="if (this.value == 'Search video') {this.value = '';}" value="<?php if(isset($search_key))echo $search_key;else echo 'Search video';?>" class="searchfield" onKeyPress="return submitenter(this,event)" /> 
</form> 

Super Globals

而且萬一你錯過了,你input標籤需要/關閉>,像/>否則就不是之前有效的標記。

0

如果顯示的URL爲/search?search_key=blabla,請嘗試使用$_POST並使用print_r($_POST),因爲CI上的默認表單方法使用的是post。

+0

謝謝,jszumski。我是這個網站的新手。 :d – dhidy 2013-04-27 03:19:04

0

爲了從查詢字符串獲取值,您必須設置true爲您的查詢字符串。

去 1)的application/config/config.php文件 2)設置查詢字符串選項真正

例。 $ config ['enable_query_strings'] = true;

比你可以得到$ _GET [ 「SEARCH_KEY」]

其他明智送超值像

mypage.com/video/search/blabla 

你會得到 「布拉布拉」 搜索功能的第一perameter在cideo控制器。