【问题标题】:Put dropdown selection in variable and use in search query将下拉选择放入变量中并在搜索查询中使用
【发布时间】:2018-01-24 20:41:09
【问题描述】:

我正在使用 2 个页面,index.phpdropdown-display.php。我在index.php 中有一个下拉列表,它将选择发送到dropdown-display.php 并过滤我的HTML 表格。我还有一个搜索框,如果已经进行了下拉选择,我希望它也可以使用查询中的下拉值来搜索已经过滤的结果。

那么如何将下面的变量$q(如果已进行下拉选择)合并到我的搜索查询中?

这是我目前拥有的...

索引.php:

<?php
if(isset($_POST['search-species'])) {

// variable that brings in search value 
    $speciesToSearch = $_POST['SpeciesToSearch'];

// query used to filter results based on search 
    $sql = "SELECT *
        FROM Example_Final_Structure
        WHERE [Species] LIKE '%".$speciesToSearch."%'
        ORDER BY [Current-SKU] ASC";

} else {

    $sql = "SELECT *
        FROM Example_Final_Structure
        ORDER BY [Current-SKU] ASC";

}
?>

index.php 中的 HTML:

<script>
// function that gets the value of the dropdown and sends it to display-dropdown.php to get results
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;

                var newTableObject = document.getElementById('millwork_table');
                sorttable.makeSortable(newTableObject);
            }
        };
        xmlhttp.open("GET","dropdown-display.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

<form name="myForm" action="">
<section id="supp_name_dropdown" onchange="hide2()" align="center" >
    <select id="selectsupp" class="supp-name" data-attribute="supp" onchange="showUser(this.value)">                
        <option value="" selected disabled>Supplier Name</option>
        <?php foreach($drop->fetchAll() as $dropdown) { ?>
            <option class="sku-<?php echo $dropdown['Supplier-Name'];?>" value="<?php echo $dropdown['Supplier-Name'];?>"><?php echo $dropdown['Supplier-Name'];?></option>
        <?php } ?>
    </select>
</section>
</form>



<section id="search-species">
<form method="post" action="index.php">

    <input name="SpeciesToSearch" class="search" type="text" placeholder="Species">
<span class="arrow"></span>
    <button type="submit" class="button" name="search-species" value="Search">Search</button><br>
</form>
</section>

下拉显示.php:

<?php
$q = ($_GET['q']); //variable that holds dropdown selection value

$sql="SELECT *
        FROM Example_Final_Structure
        WHERE [Supplier-Name] = '$q'";

?>

【问题讨论】:

  • @IncredibleHat 这就是我所拥有的所有 sql 查询,只是带有下拉选择的变量和显示过滤数据的查询....我有 html 来显示表格结果但是不认为这在我的问题中起了很大的作用
  • @IncredibleHat 我只是不确定如何使用我的$q 变量,它包含下拉选择,在我已经拥有的搜索查询中......我已经拥有过滤功能通过下拉选择搜索或过滤.....我只是希望能够将该变量放入我的搜索查询中,因为现在,如果我通过下拉过滤,然后搜索,显示的结果将完全忽略最初在下拉
  • 好的。我懂了。好吧,sql-server 与 mysqli 或 PDO 不同,所以是的,其他人将不得不介入如何正确保护输入变量免受 sql 注入和转义问题。抱歉,我误解了这个问题。
  • @IncredibleHat 好吧,没问题!

标签: javascript php sql-server xmlhttprequest


【解决方案1】:

您可以在搜索表单中添加&lt;input type="hidden" id="searchsupp" name="searchsupp" value=""/&gt;

然后在您的showUser 函数中,将接收到的值注入隐藏输入。 document.getElementById("searchsupp").value = str;

然后您可以通过将其添加到查询中来在搜索中使用此值

"SELECT *
 FROM Example_Final_Structure
 WHERE ".($searchsupp != "" ? "[Supplier-Name] = '".$searchsupp."' AND ")."[Species] LIKE '%".$speciesToSearch."%'
 ORDER BY [Current-SKU] ASC"

其中$searchsupp 是您从发布的数据中创建的变量。

【讨论】:

  • 感谢您的回复!我似乎对此有疑问...是否将&lt;input type="hidden" etc... 添加到index.php 中的当前搜索表单中?另外,该查询和$searchsupp 变量是否应该在index.php 或我的dropdown-display.php 页面中?
  • 它应该都在index.php 文件中。 $searchsupp应该被赋予$_POST['searchsupp']的值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
相关资源
最近更新 更多