2014-12-02 1 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 

변수가 있으므로이 변수는 사용자가 위치를 사용 사실 경우 "true"로된다 :처럼 뭔가를 생각 그들을 연결 ... 그래서 PHP 문에이 같은 것을 추가하려고 :

비록 작동하지 않습니다. 나는 몇몇 도움 녀석을 평가할 것입니다! 미리 감사드립니다.

+0

이유는 단지 한 형태 대신 2를 사용하지 않는? 2 개의 양식을 1 개의 양식으로 결합 할 수 있으며 제출할 때 위치와 검색 정보를 제출합니다. –

+0

두 양식이 페이지의 두 다른 섹션에있는 두 개의 다른 div에 있기 때문에 두 양식을 결합하는 방법을 모르겠다. html이 영향을받지 않고 어떻게이 작업을 수행 할 수 있습니까 ?? 도움을 주신 덕분에 – Frank

답변

1

여기가 약간의 트릭입니다.

<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> 

은 자바 스크립트가 위치에 검색 필드를 추가합니다 :

<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> 

다음이 자바 스크립트를 추가 위치 양식과 검색 양식 searchForm에 ID locationForm 추가, 그래서 다음과 같습니다 양식과 비자를 제출하십시오. 따라서 양식 중 하나를 제출할 때마다 항상 두 값을 모두 갖게됩니다.

편집에서

당신의 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에 문제가 있습니까? 참고로, 나는 PHP 코드의 마지막 블록을 내 질문에 추가하지 않았다. – Frank

+0

미안하다. 왜냐하면 내가'action'을 변경했기 때문이다. 내 대답을 업데이 트하면,'index'.php? go'로 위치 양식의'action' 속성을 다시 변경하면됩니다. –

+0

정말 죄송합니다, 같은 결과 ... 또한 PHP에서 "이동"작업을 변경했는데 ...하지만 사용자가 검색 필드와 무언가 모두에 입력하면 결과가 있어야한다고 말해줘. 두 쿼리가 동시에 실행될 수 있습니까? – Frank

관련 문제