【问题标题】:How to echo on page which is included in index file?如何在索引文件中包含的页面上回显?
【发布时间】:2015-11-20 16:46:45
【问题描述】:

我有一个 index.php 文件,其正文如下:

<body>
        <!-- include navigation -->
        <header>
            <?php include 'navi.php'; ?>
        </header>   

        <!-- include content -->
        <section>
            <div>
                <?php
                if (!empty($_GET['page'])) {
                    if (!file_exists($_GET['page'] . ".php")) {
                        include("home.php");
                    } else {
                        include($_GET['page'] . ".php");
                    }
                } else {
                    include("home.php");
                }
                ?>
            </div>
        </section>
        <!-- include footer -->
        <?php include 'footer.html'; ?>
    </body>

还有一个 nav.php:

<nav>
    <ul>
        <li>
            <img onclick="window.location.href = '?page=home'" id="logo" src="./images/logo.png">
        </li>
        <li>
            <a href="?page=search">Search Events</a>
        </li>
        <li>
            <a href="?page=create">Create Event</a>
        </li>
        <li>
            <a href="?page=account">My Account</a>
        </li>
        <li>
            <a href="logout.php" >
                <img id="logout" src="./images/logout.png">
            </a>
        </li>
    </ul>
</nav>

在搜索页面中,我有一个简单的表单来搜索给定名称和类型的数据库。如何在搜索文件中回显我的结果?如果我只是 include './search.php'; 缺少 index.php 中的整个代码,不幸的是我不能像这样包含:include 'index.php?page=search';

【问题讨论】:

  • include 'index.php?page=search'; - include 不起作用,大约一天前有一个类似的问题。
  • 是的,我知道,我只是想知道是否有这样的功能?
  • 我对此表示怀疑。如果你愿意,你可以使用 JS/Ajax,这样就可以了。
  • 是的,我将如何使用 JS/Ajax?
  • 看看这个 Q&A stackoverflow.com/q/16637076 并使用“使用 ajax php 显示来自数据库的结果”作为关键字进一步研究。那里有很多东西。另一个是w3schools.com/php/php_ajax_database.asp,许多人对 W3 学校不屑一顾,但它确实有效。我自己之前试过。那是如果你试图从一个数据库中获取东西。

标签: php html


【解决方案1】:

您可以使用header("location:index.php?page=search") 代替include "index.php?page=search",然后在您的index.php 文件中使用include $_GET['page'].'php'。当然,你应该首先验证$_GET['page']实际上是一个集合变量。

【讨论】:

  • 我每天都使用这种技术。省去了很多麻烦。
猜你喜欢
  • 1970-01-01
  • 2020-05-29
  • 2014-06-09
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多