2011-03-31 46 views
1

我希望我的搜索欄完成搜索,但是,我希望目標是「整頁」而不是完全移動到另一個頁面?在整個頁面中搜索目標

<div id="search_anything"> 
    <form action="/search.php" method="get" id='searchForm'> 
     <div class="searchFormDiv"> 
      <input type="text" name="search" value="Search Mail..." id="search" onfocus="if($(this).attr('value').indexOf('...') >= 0) $(this).attr('value',''); $(this).select();" /> 
     </div> 
    </form> 
</div> 
+1

你的意思是你想要在同一個頁面上搜索並顯示結果,而不是重定向到另一個頁面來顯示結果? – 2011-03-31 21:11:09

回答

0
<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="get" id='searchForm'> 

不要忘記,大部分時間你需要處理在同一頁上的表格太(或在該頁上包括一個獨立的形式處理腳本)。

例如,如果你想處理其他PHP文件的形式,可以粘貼此表格上面的任何地方:

<?php if(isset($_GET['search'])) include 'formprocessor.php'; ?> 
+0

當我輸入該代碼時,我得到以下鏈接: - http://liveternet.com/?search=(My search),然而,我想要:http://liveternet.com/search.php?search=(My搜索)<<包括搜索在同一網頁中完成:)(並且未被重定向到另一個) – user686512 2011-03-31 22:10:44

+0

@ user686512當您不想重定向到搜索時,爲什麼要在action屬性中搜索** search.php **。 PHP的?通過** liveternet.com /?search = mysearch **,您將可以在同一頁面上處理表單。如果你真的想要這兩者,你可以用Apache的mod_rewrite(在.htaccess文件中)重寫。 – DADU 2011-04-01 00:13:20

0

當我輸入的代碼我得到以下鏈接: - liveternet.com/ ?搜索=(我的搜索),但是,我想:liveternet.com/search.php?search=(My Search)< <包括搜索在同一網頁中完成:)(並沒有重定向到另一個)

0

使用jQuery:

$('form').submit(function() { 
    $.post(
    'do_query.php',   // File with query 
    { num: '1', str: 'string' }, // Vars passed as post 
    function(responseText){  // Callback 
     $('#your_div').html(responseText); // Load content results from do_query page. 
    }, 
    "html"    // Set type as html 
); 
    preventDefault();   // Prevent form from submiting 
}); 

這應該工作。請提供反饋。

+0

爲什麼要在一段JavaScript中放置一個PHP塊?除此之外,它不能用於禁用JavaScript。 – DADU 2011-04-01 13:21:21

+0

這不起作用。 – user686512 2011-04-01 19:16:31

+0

已編輯。現在使用ajax請求。 – IzzyCooper 2011-04-27 22:20:25