gpt4 book ai didi

php - MySQL/PHP : connecting two individual search forms

转载 作者:行者123 更新时间:2023-11-29 12:32:38 25 4
gpt4 key购买 nike

我的情况:我有两种不同的形式。一种常见的搜索表单,用户可以按名称/描述搜索产品,另一种表单允许用户按位置(邮政编码和城市)搜索产品。

这是我的搜索表单的 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”,所以如果这个变量是true并且用户正在使用位置 - 搜索表单,然后连接它们......所以我尝试添加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
}}}}

但它不起作用。我会很感激一些帮助的人!提前致谢。

最佳答案

这里有一个小技巧。将 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

关于php - MySQL/PHP : connecting two individual search forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27250668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com