简体   繁体   English

PHP 带有选择字段的完整搜索表单

[英]PHP Full search form with select fields

having this problem for more than three days to fix.有这个问题超过三天才能解决。

I'm creating a form that lets the user search with some conditions.我正在创建一个表单,让用户在某些条件下进行搜索。

Why the values of company and category are not passing to the URL when using the get method ?为什么在使用 get 方法时公司和类别的值没有传递到 URL

<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
      <div class="form-group" id="main-searchbox">
           <input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
      </div>
      <div class="selection-container">
           <select name="company" class="header-selection" form="main-searchbox">
                   <option value="company" name="company">Company</option>
           </select>
      </div>
      <div class="selection-container">
           <select name="category" class="header-selection" form="main-searchbox">
                   <?php 
                    // Make the query:
                    $sql = "select Business_Category from business_categories order by Business_Category ASC";
                    $result = mysqli_query($conn, $sql);
                    if (mysqli_num_rows($result) > 0) {
                    // output data of each row
                       while ($row = mysqli_fetch_assoc($result)) {
                                      echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
                       }
                    } else { echo "----";} // End
                              ?>         
            </select>
        </div>
        <input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
 </form>

It must be quite straight forward to fix this and with your feedback can fix that easier.解决这个问题必须非常直接,并且根据您的反馈可以更容易地解决这个问题。

Thank you for your time.感谢您的时间。

It worked by removing form="main-searchbox" present in both <select> elements (inside of <div class="selection-container"> ) as there is not a matching id with that name on the <form> , as RiggsFolly said in the comments.它的工作form="main-searchbox"是删除两个<select>元素(在<div class="selection-container"> )中存在的form="main-searchbox" ,因为<form>上没有与该名称匹配的 ID,如RiggsFolly在评论中说。

See the working code:查看工作代码:

<form class="main-searchbox-container" role="search" method="get" action="search-results.php">
    <div class="form-group" id="main-searchbox">
        <input id="target" type="text" class="form-control main-search-input" placeholder="Search..." name="target" value="<?php if (isset($_POST['target'])) echo $_POST['target']; ?>" />
    </div>
    <div class="selection-container">
        <select name="company" class="header-selection">
            <option value="company" name="company">Company</option>
        </select>
    </div>
    <div class="selection-container">
        <select name="category" class="header-selection">
            <?php 
                 // Make the query:
                 $sql = "select Business_Category from business_categories order by Business_Category ASC";
                 $result = mysqli_query($conn, $sql);
                 if (mysqli_num_rows($result) > 0) {
                     // output data of each row
                     while ($row = mysqli_fetch_assoc($result)) {
                         echo '<option value="'.$row['Business_Category'].'" name="'.$row['Business_Category'].'">'.$row['Business_Category'].'</option>';
                     }
                 } else { echo "----";} // End
            ?>         
            </select>
        </div>
        <input id="submit" class="btn btn-default main-search-button" type="submit" name="submit" value="Search">
 </form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM