2014-12-02 34 views
0

我的情況: 我有兩個不同的形式。用戶可以通過其名稱/描述搜索產品的一種常見搜索形式,以及允許用戶通過其位置(郵編和城市)搜索產品的另一種形式。MySQL/PHP:連接兩個單獨的搜索表格

這是我的搜索表單的HTML:

<form name="searchform" method="post" action="index.php?go" class="searchform">    
       <input type="text" name="search" value="" placeholder="Suchen..." class="field_search" id="tags"> 
       </form> 

,這是我的位置,搜索表單的HTML:

<form class="location" method="post" action="index.php?go_location"> 
      <input type="image" src="img/location/location.png" width="30" height="30" id="location_image" title="Ortung aktivieren"/> 
      <input type="text" size="18" placeholder="PLZ, Ort" name="location" id="location" title="Standort angeben"/> 
      <input type="image" name="" value="" src="img/location/go.png" width="30" height="30" id="location_submit"/> 
     </form> 

和相應的PHP:

if(isset($_POST['search'])){ 
if(isset($_GET['go'])){ 
if(preg_match("/[A-Z | a-z]+/", $_POST['search'])){ 
$input=$_POST['search']; 

$currently_searching = true; 

//connect to db 

$sql="SELECT * FROM table WHERE Name LIKE '%".$input."%' OR Description LIKE '%".$input."%'"; 

//echo results 
}}}} 

elseif(isset($_POST['location'])){ 
if(isset($_GET['go_location'])){ 
$input_location=$_POST['location']; 

$currently_locationing = true; 

$sql="SELECT * FROM table WHERE Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%'"; 

//echo results 
}}} 

現在,個別,他們工作正常。 我想要實現的是將這兩種形式連接起來,讓已經搜索某個字符串的用戶(通過公共搜索表單)使用位置搜索表單將結果縮小到與給定字符相對應的結果郵政編碼...

我希望這是明確的。我想是這樣的:如果用戶使用通用搜索表單中,

$currently_searching 

變量變成「真」,所以如果這個變量爲真用戶使用位置 - 搜索表單,然後連接他們...所以我試圖添加這樣的php語句:

elseif(isset($_POST['location']) && $currently_searching == true){ 
if(isset($_GET['go_location']) && $currently_searching == true){ 
if($currently_searching == true){ 
$input_location=$_POST['location']; 

$currently_locationing = true; 

//connect to db 

$sql="SELECT * FROM table WHERE (Name LIKE '%".$input."%' OR Description LIKE '%".$input."%') AND (Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%')"; 

//echo results 
}}}} 

雖然它不工作。我會感謝一些幫助傢伙!提前致謝。

+0

爲什麼不只是使用1種形式,而不是2?您可以將2種形式合併爲1種形式,當您提交時,它會提交位置和搜索信息。 – 2014-12-02 13:30:07

+0

我不知道如何結合這兩種形式而不搞亂HTML,因爲這兩種形式在頁面的兩個不同部分有兩個不同的div。我怎樣才能做到這一點,而不受影響? – Frank 2014-12-02 13:50:50

回答

1

這是一個小竅門。該ID locationForm添加到位置的形式和searchForm您的搜索形式,所以它看起來是這樣的:

<form id="locationForm" class="location" method="post" action="index.php?go"> 
    <input type="image" src="img/location/location.png" width="30" height="30" id="location_image" title="Ortung aktivieren"/> 
    <input type="text" size="18" placeholder="PLZ, Ort" name="location" id="location" title="Standort angeben"/> 
    <input type="image" name="" value="" src="img/location/go.png" width="30" height="30" id="location_submit"/> 
</form> 

<form id="searchForm" name="searchform" method="post" action="index.php?go" class="searchform">    
    <input type="text" name="search" value="" placeholder="Suchen..." class="field_search" id="tags"> 
</form> 

然後添加此javascript:

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script> 

    $(document).on('submit', '#locationForm, #searchForm',function(e){ 

     var locationInput = $('#locationForm input[name="location"]').clone(); 
     locationInput.attr('type','hidden'); 

     var searchInput = $('#searchForm input[name="search"]').clone(); 
     searchInput.attr('type','hidden'); 

     $('#locationForm').prepend(searchInput); 
     $('#searchForm').prepend(locationInput); 
    }); 
</script> 

JavaScript的搜索字段添加到位置形式和反向提交之前。所以,無論何時提交其中一個表單,您都將始終具有兩個值。

編輯

在你corresponding.php如果有需要兩個單獨的查詢,你可以使用這樣的事情。

if(isset($_POST['search'])) 
{ 
    $sql="SELECT * FROM table WHERE Name LIKE '%".$input."%' OR Description LIKE '%".$input."%'"; 
    //Execute query 
    //Fetch results 
} 

if(isset($_POST['location'])) 
{ 
    $sql="SELECT * FROM table WHERE Postcode LIKE '%".$input_location."%' OR City LIKE '%".$input_location."%' OR Combined LIKE '%".$input_location."%' OR Combined2 LIKE '%".$input_location."%'"; 
    //Execute query 
    //Fetch results 
} 

//Combine results of search and location query 

//echo results 

或者,如果有可能執行一個查詢,你可以使用這個:

if(isset($_POST['search']) && isset($_POST['location'])) 
{ 
    $sql="HERE YOUR QUERY WHERE YOU CAN USE $_POST['search'] AND $_POST['location']"; 
    //Execute query 
    //Fetch results 
} 

//Combine results of search and location query 

//echo results 
+0

感謝您的幫助!不幸的是有些東西沒有用。位置表單不會返回任何東西......並且常見的搜索表單照常工作。也許有一個問題在PHP?僅供參考,我沒有在我的問題 – Frank 2014-12-02 14:30:01

+0

中添加最後一塊php代碼我很抱歉,那是因爲我更改了「操作」。我更新了我的答案,您只需要將位置表單的'action'屬性更改回'index.php?go'。 – 2014-12-02 14:33:09

+0

我真的很抱歉,同樣的結果...我也嘗試將操作更改爲在PHP中「去」...但告訴我,結果應該是,如果用戶在搜索字段中輸入內容並點擊提交其中,兩個查詢會同時運行? – Frank 2014-12-02 14:43:59