Php 如何根据从HTML筛选表单接收的变量值更改SQL搜索查询?
我有一个表单块,它在下拉列表中有搜索参数,用户选择至少一个选择选项并单击Php 如何根据从HTML筛选表单接收的变量值更改SQL搜索查询?,php,mysql,ajax,Php,Mysql,Ajax,我有一个表单块,它在下拉列表中有搜索参数,用户选择至少一个选择选项并单击搜索按钮,这将生成一个表单帖子提交给JS脚本,JS脚本获取所有或部分选择值的值,并根据选择选项修改SQL查询 但问题是我需要结合选择和手动修改sql查询来执行和检索结果。但是我想做一些事情,根据SQL查询接收到的参数自动修改它 请引导我 下面是我的过滤器的外观 下面是JS脚本的代码loadtable.JS $(document).ready(function(){ var delay = 1000; /
搜索按钮,这将生成一个表单帖子提交给JS脚本,JS脚本获取所有或部分选择值的值,并根据选择选项修改SQL查询
但问题是我需要结合选择和手动修改sql查询来执行和检索结果。但是我想做一些事情,根据SQL查询接收到的参数自动修改它
请引导我
下面是我的过滤器的外观
下面是JS脚本的代码loadtable.JS
$(document).ready(function(){
var delay = 1000;
// Campaign Submit Info
$('[name="search_submit"]').click(function(e){
e.preventDefault();
var lead_status = $('#filterformpost').find('#lead_status_select option:selected').val();
var campaign_status = $('#filterformpost').find('#campaign_status_select option:selected').val();
var company_name = $('#filterformpost').find('#company_name_select option:selected').val();
var tech_area = $('#filterformpost').find('#tech_area_select option:selected').val();
var firm_size = $('#filterformpost').find('#firm_size_select option:selected').val();
var firm_type = $('#filterformpost').find('#firm_type_select option:selected').val();
var country_name = $('#filterformpost').find('#country_name_select option:selected').val();
var state_name = $('#filterformpost').find('#state_name_select option:selected').val();
var start_date = $('#filterformpost').find('#start_date_search').val();
var end_date = $('#filterformpost').find('#end_date_search').val();
$.ajax({
type: "POST",
url: "http://localhost/CRM/server/search.php",
data: {
"lead_status":lead_status,
"campaign_status":campaign_status,
"company_name":company_name,
"tech_area":tech_area,
"firm_size":firm_size,
"firm_type":firm_type,
"country_name":country_name,
"state_name":state_name,
"start_date":start_date,
"end_date":end_date
},
beforeSend: function() {
$('.message_box').html(
'<img src="tenor.gif" width="40" height="40"/>'
);
},
success: function(data)
{
setTimeout(function() {
$('.message_box').html(data);
}, delay);
}
});
});
我正在尝试以下建议的解决方案:
dashboard.html中节标记的一部分
<!-- Filters -->
<div class="row">
<div class="col">
<label><p><b>Select Filter</b></p></label>
</div>
</div>
<form action='' method='POST' class='filterformpost' id='filterformpost'>
<div class="row">
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-4">
Lead Status:
</div>
<div class="col span-2-of-4">
<select id='lead_status_select'><option>Select</option>
<?php
echo "<option value='All'>All</option>";
echo "<option value='Active'>Active Leads</option>";
echo "<option value='Paused'>Paused Leads</option>";
echo "<option value='Expired'>Expired Leads</option>";
echo "<option value='Unsubscribed'>Unsubscribed</option>";
?>
</select>
</div>
</div>
</div>
<div class="col span-1-of-3">
<div class="row">
<div class="col span-1-of-4">
Campaign Status:
</div>
<div class="col span-2-of-4">
<select id='campaign_status_select'><option>Select</option>
<?php
echo "<option value='All'>All</option>";
echo "<option value='Active'>Active</option>";
echo "<option value='Paused'>Paused</option>";
echo "<option value='Expired'>Expired</option>";
echo "<option value='Unsubscribed'>Unsubscribed</option>";
?>
</select>
</div>
</div>
</div>
<div class="col span-1-of-3">
<div class="row">
<div class="col span-1-of-3">
Company Name:
</div>
<div class="col span-2-of-3">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_main_lead_info ORDER By Company ASC";
$result = $conn -> query ($sqlSelect);
echo "<select id='company_name_select'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[Company]'> $row[Company] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-4">
State:
</div>
<div class="col span-2-of-4">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_state_info ORDER By StateName ASC";
$result = $conn -> query ($sqlSelect);
$result = $conn -> query ($sqlSelect);
echo "<select id='state_name_select' name='StateName'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[StateName]'> $row[StateName] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
<div class="col span-1-of-3">
<div class="row">
<div class="col span-1-of-4">
Country:
</div>
<div class="col span-2-of-4">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_country_info ORDER By CountryName ASC";
$result = $conn -> query ($sqlSelect);
$result = $conn -> query ($sqlSelect);
echo "<select id='country_name_select' name='CountryName'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[CountryName]'> $row[CountryName] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
<div class="col span-1-of-3">
<div class="row">
<div class="col span-1-of-3">
Firm Type:
</div>
<div class="col span-2-of-3">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_firm_type_info ORDER By FirmType_Value ASC";
$result = $conn -> query ($sqlSelect);
$result = $conn -> query ($sqlSelect);
echo "<select id='firm_type_select' name='FirmType'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[FirmType_Value]'> $row[FirmType_Value] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-4">
Firm Size:
</div>
<div class="col span-2-of-4">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_firm_size_info ORDER By FirmSize_Id ASC";
$result = $conn -> query ($sqlSelect);
$result = $conn -> query ($sqlSelect);
echo "<select id='firm_size_select' name='FirmSize'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[FirmSize_Value]'> $row[FirmSize_Value] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-3">
Tech Area:
</div>
<div class="col span-2-of-3">
<?php
include('./server/connection.php');
$sqlSelect="SELECT * FROM tbl_tech_area_info ORDER By TechAreaName ASC";
$result = $conn -> query ($sqlSelect);
$result = $conn -> query ($sqlSelect);
echo "<select id='tech_area_select' name='TechAreaName'>";
echo "<option>select</option>";
echo "<option value='All'>All</option>";
while ($row = mysqli_fetch_array($result)) {
echo "<option value='$row[TechAreaName]'> $row[TechAreaName] </option>";
}
echo "</select>";
?>
</div>
</div>
</div>
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-4">
Start Date:
</div>
<div class="col span-3-of-4">
<?php
echo "<input type='date' id='start_date_search' name='startdate'>";
?>
</div>
</div>
</div>
<div class="col span-1-of-4">
<div class="row">
<div class="col span-1-of-4">
End Date:
</div>
<div class="col span-3-of-4">
<?php
echo "<input type='date' id='end_date_search' name='enddate'>";
?>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col span-1-of-3">
<div class="row">
<div class="col span-3-of-4">
</div>
</div>
</div>
<div class="col span-1-of-3">
<div class="row">
<div class="col span-3-of-4">
<div class="row">
<div class="col span-1-of-3">
<label></label>
</div>
<div class="col span-2-of-3">
<input type="submit" name='search_submit' value="Search">
</div>
</div>
</div>
</div>
</div>
</div>
</form>
<div class="row">
<div class="col span-1-of-3">
<label></label>
</div>
<div class="col span-2-of-3">
<div class="message_box" style="margin-left: 60px;">
</div>
</div>
</div>
而loadtable.js
文件是:
$(document).ready(function(){
var delay = 1000;
// Campaign Submit Info
$('[name="search_submit"]').click(function(e){
e.preventDefault();
var lead_status = $('#filterformpost').find('#lead_status_select option:selected').val();
var campaign_status = $('#filterformpost').find('#campaign_status_select option:selected').val();
var company_name = $('#filterformpost').find('#company_name_select option:selected').val();
var tech_area = $('#filterformpost').find('#tech_area_select option:selected').val();
var firm_size = $('#filterformpost').find('#firm_size_select option:selected').val();
var firm_type = $('#filterformpost').find('#firm_type_select option:selected').val();
var country_name = $('#filterformpost').find('#country_name_select option:selected').val();
var state_name = $('#filterformpost').find('#state_name_select option:selected').val();
var start_date = $('#filterformpost').find('#start_date_search').val();
var end_date = $('#filterformpost').find('#end_date_search').val();
console.log(lead_status)
console.log(campaign_status)
console.log(company_name)
console.log(tech_area)
console.log(firm_size)
console.log(firm_type)
console.log(country_name)
console.log(state_name)
console.log(start_date)
console.log(end_date)
$.ajax({
type: "POST",
// url: "https://tribalyze.com/CRM/server/login.php",
url: "server/search.php",
data: {
"lead_status":lead_status,
"campaign_status":campaign_status,
"company_name":company_name,
"tech_area":tech_area,
"firm_size":firm_size,
"firm_type":firm_type,
"country_name":country_name,
"state_name":state_name,
"start_date":start_date,
"end_date":end_date
},
beforeSend: function() {
$('.message_box').html(
'<img src="tenor.gif" width="40" height="40"/>'
);
},
success: function(data)
{
setTimeout(function() {
$('.message_box').html(data);
}, delay);
}
});
});
$.post('./server/search.php', function(result){
console.log('Hello Console');
$.each(result, function(){
console.log(result);
});
});
});
$(文档).ready(函数(){
无功延迟=1000;
//活动提交信息
$('[name=“search_submit”]')。单击(函数(e){
e、 预防默认值();
var lead_status=$(“#filtermerformpost”).find(“#lead_status_选择选项:selected”).val();
var campaign_status=$(“#filtermerformpost”).find(“#campaign_status_选择选项:selected”).val();
var company_name=$(“#filtermerformpost”).find(“#company_name_select option:selected”).val();
var tech_area=$(“#filtermerformpost”).find(“#tech_area_选择选项:selected”).val();
var firm_size=$(“#filtermperformpost”).find(“#firm_size_选择选项:selected”).val();
var firm_type=$(“#filtermperformpost”).find(“#firm_type_选择选项:selected”).val();
var country_name=$(“#filtermerformpost”).find(“#country_name_选择选项:selected”).val();
var state_name=$('#filtermerformpost').find('#state_name_select option:selected').val();
var start_date=$('#filtermerformpost').find('#start_date_search').val();
var end_date=$('#filtermerformpost').find('#end_date_search').val();
控制台日志(lead_状态)
控制台日志(活动状态)
console.log(公司名称)
控制台日志(技术区域)
控制台日志(固定大小)
控制台日志(固定类型)
console.log(国家/地区名称)
console.log(状态名称)
console.log(开始日期)
console.log(结束日期)
$.ajax({
类型:“POST”,
//url:“https://tribalyze.com/CRM/server/login.php",
url:“server/search.php”,
数据:{
“潜在客户状态”:潜在客户状态,
“活动状态”:活动状态,
“公司名称”:公司名称,
“技术区”:技术区,
“固定大小”:固定大小,
“公司类型”:公司类型,
“国家名称”:国家名称,
“州名”:州名,
“开始日期”:开始日期,
“结束日期”:结束日期
},
beforeSend:function(){
$('.message_box').html(
''
);
},
成功:功能(数据)
{
setTimeout(函数(){
$('.message_box').html(数据);
},延误);
}
});
});
$.post('./server/search.php',函数(结果){
log('Hello console');
$.each(结果,函数(){
控制台日志(结果);
});
});
});
您可能需要检查每个变量是否传递了任何值,并动态构造查询:
<?php
include('connection.php');
$sqlFlag = 0;
function queryDelimiter(){
global $sqlFlag;
if ($sqlFlag == 0){
$sqlFlag = 1;
return ' WHERE ';
}else{
return ' AND ';
}
}
$selectSQL = "SELECT * FROM tbl_main_lead_info";
if(isset($_POST['campaign_name']) and strlen(trim($_POST['campaign_name'])) > 0){
$selectSQL .= queryDelimiter()."campaign_name = '".$_POST['campaign_name']."'";
}
if(isset($_POST['lead_status']) and strlen(trim($_POST['lead_status'])) > 0){
$selectSQL .= queryDelimiter()."lead_status = '".$_POST['lead_status']."'";
}
$selectSQL .= " ORDER BY campaign_name";
您希望在SQL中使用AND或搜索组合吗?所有字段都是同一个表的一部分,或者数据分布在不同的表中?这意味着在elseNo下有这么多SQL查询,查询将是一个,主要问题是您使用了多少个数据库表。@mitkosoft,所有值都是同一个值的一部分table@mitkosoft我在数据库中使用单表感谢此if else将帮助我检查当我根据选择条件获得行时,如何发送行以在Dashboard中显示表。HTMLH我们如何在php中使用AJAX显示搜索查询的结果?@RINKUYADAV,这是完全不同的问题。您的PHP脚本search.PHP
必须从SQL结果生成JSON,您需要将其传递到页面echo json\u encode($sql\u result)
将完成这项工作,只需准备一个合适的数组即可。请看这里的更多内容:这就是我所需要的,感谢这个链接和上面的解决方案。
$(document).ready(function(){
var delay = 1000;
// Campaign Submit Info
$('[name="search_submit"]').click(function(e){
e.preventDefault();
var lead_status = $('#filterformpost').find('#lead_status_select option:selected').val();
var campaign_status = $('#filterformpost').find('#campaign_status_select option:selected').val();
var company_name = $('#filterformpost').find('#company_name_select option:selected').val();
var tech_area = $('#filterformpost').find('#tech_area_select option:selected').val();
var firm_size = $('#filterformpost').find('#firm_size_select option:selected').val();
var firm_type = $('#filterformpost').find('#firm_type_select option:selected').val();
var country_name = $('#filterformpost').find('#country_name_select option:selected').val();
var state_name = $('#filterformpost').find('#state_name_select option:selected').val();
var start_date = $('#filterformpost').find('#start_date_search').val();
var end_date = $('#filterformpost').find('#end_date_search').val();
console.log(lead_status)
console.log(campaign_status)
console.log(company_name)
console.log(tech_area)
console.log(firm_size)
console.log(firm_type)
console.log(country_name)
console.log(state_name)
console.log(start_date)
console.log(end_date)
$.ajax({
type: "POST",
// url: "https://tribalyze.com/CRM/server/login.php",
url: "server/search.php",
data: {
"lead_status":lead_status,
"campaign_status":campaign_status,
"company_name":company_name,
"tech_area":tech_area,
"firm_size":firm_size,
"firm_type":firm_type,
"country_name":country_name,
"state_name":state_name,
"start_date":start_date,
"end_date":end_date
},
beforeSend: function() {
$('.message_box').html(
'<img src="tenor.gif" width="40" height="40"/>'
);
},
success: function(data)
{
setTimeout(function() {
$('.message_box').html(data);
}, delay);
}
});
});
$.post('./server/search.php', function(result){
console.log('Hello Console');
$.each(result, function(){
console.log(result);
});
});
});
<?php
include('connection.php');
$sqlFlag = 0;
function queryDelimiter(){
global $sqlFlag;
if ($sqlFlag == 0){
$sqlFlag = 1;
return ' WHERE ';
}else{
return ' AND ';
}
}
$selectSQL = "SELECT * FROM tbl_main_lead_info";
if(isset($_POST['campaign_name']) and strlen(trim($_POST['campaign_name'])) > 0){
$selectSQL .= queryDelimiter()."campaign_name = '".$_POST['campaign_name']."'";
}
if(isset($_POST['lead_status']) and strlen(trim($_POST['lead_status'])) > 0){
$selectSQL .= queryDelimiter()."lead_status = '".$_POST['lead_status']."'";
}
$selectSQL .= " ORDER BY campaign_name";