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