php 根据浏览的页面,在Bootstrap中预先选择下拉菜单选项

eqoofvh9  于 2022-11-21  发布在  PHP
关注(0)|答案(1)|浏览(175)

我有一个PHP网站,index.php调用不同的网页,如下所示:
index.php?page=cars
然后:

<html code>
<?php
$pagesOk = array('cars, 'boats', 'trucks);
...
if (in_array($_GET['page'], $pagesOk)) {
   require_once('pages/'.$page.'.php');
}

我还在index.php文件的顶部提供了一个searchbox(用JQuery自动完成填充),以便可以从所有子页面访问它。

<nav class="searchbar input-group">
   <input type="hidden" name="search_param" value="all" id="search_param">
   <input class="reset" type="search" id="searchInput" autocomplete="none" data-clearButton >
   <button class="reset cd-nav-trigger" aria-label="Toggle menu"></button>
   <div class="input-group-btn search-panel">
     <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
       <span id="search_concept">All content<span class="caret"></span>
     </button>
     <ul class="dropdown-menu mode-recherche" role="menu" style="top: 10px !important; left: -10px !important">
          <li><a href="#cars">Cars</a></li>
          <li><a href="#boats">Boats</a></li>
          <li><a href="#trucks">Trucks</a></li>
          <li class="dropdown-divider"></li>
          <li><a href="#all">All content</a></li>
     </ul>
  </div>

我想现在的下拉菜单将自动选择到当前页面。
这意味着,如果用户在index.php?page=boats上,则会在搜索框中选择下拉菜单Boats,这样他就可以默认只搜索船只。

vyu0f0g1

vyu0f0g11#

您可以传递页面名称,然后根据$page名称传递selected选项

以下是包含php的完整代码:

<?php
    $pagesOk = array('cars','boats', 'trucks');
    $page = $_GET['page'];
    if (in_array($page, $pagesOk)) {
        # do somthing
        # require_once('pages/'.$page.'.php');
    }
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap 5 Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
  
  <nav class="navbar navbar-expand-sm bg-light navbar-light">
    <div class="container">
      <a class="navbar-brand" href="javascript:void(0)">LOGO</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#mynavbar">
        <span class="navbar-toggler-icon"></span>
      </button>
      <div class="collapse navbar-collapse" id="mynavbar">
        <ul class="navbar-nav me-auto">
          <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)">Pages</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="javascript:void(0)">Contacts</a>
          </li>
        </ul>
        <form class="d-flex">
          <select class="form-select">
            <option>All content</option>
            <option <?=$page == 'cars' ? "selected" : ""; ?>>Cars</option>
            <option <?=$page == 'boats' ? "selected" : ""; ?>>Boats</option>
            <option <?=$page == 'trucks' ? "selected" : ""; ?>>Trucks</option>
          </select>
        </form>
      </div>
    </div>
  </nav>
  <div class="container mt-3">
    <h3>Some body text</h3>
    <p>You can also include forms inside the navigation bar.</p>
  </div>
</body>
</html>

相关问题