我的情况: 我有两个不同的形式。用户可以通过其名称/描述搜索产品的一种常见搜索形式,以及允许用户通过其位置(邮编和城市)搜索产品的另一种形式。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
}}}}
虽然它不工作。我会感谢一些帮助家伙!提前致谢。
为什么不只是使用1种形式,而不是2?您可以将2种形式合并为1种形式,当您提交时,它会提交位置和搜索信息。 – 2014-12-02 13:30:07
我不知道如何结合这两种形式而不搞乱HTML,因为这两种形式在页面的两个不同部分有两个不同的div。我怎样才能做到这一点,而不受影响? – Frank 2014-12-02 13:50:50